Document use of futures with C++17 structured bindings and other C++17/20 idioms

Issue #469 resolved
Colin MacLean created an issue

The C++ standard specifies that structured bindings can use get<i>(e), found by ADL, to enable structured bindings for a custom class. By providing this function in the upcxx namespace for upcxx::future or as a hidden friend in upcxx::future, we can enable this functionality for UPC++ futures.

upcxx::future<int,double> fut = /* ... */;
auto [a,b] = fut;

Comments (8)

  1. Dan Bonachea

    At first glance this proposal seemed like a good idea, but after some further thought I realized that future::wait() and future::result() already provide the requested functionality. I.e., the following already works (in -std=c++17 mode):

      upcxx::future<int,double> fut = 
        upcxx::rpc(0,[]() { return upcxx::make_future(42,3.14); });
    
      auto [a,b] = fut.wait();
      std::cout << a << ", " << b << std::endl;
    
      auto [c,d] = fut.result();
      std::cout << c << ", " << d << std::endl;
    

    Similarly future::wait_reference() and future::result_reference() also work in the way you'd expect, declaring const references to the future elements and eliding data copy.

    I think this already seems like the right design, because each of these four cases make sense under different circumstances, and I'd prefer the user be explicit about what they want, rather than us arbitrarily choosing one semantic. I'd also be opposed to standardizing any implicit syntax that hid a wait operation, since those have such a large potential impact on application performance.

    @Colin MacLean assuming you agree, we can convert this to a documentation issue to add an example of this idiom somewhere in our training materials. We already have some examples that accomplish a similar effect using C++11 std::tie, but C++17 makes this much more convenient with auto structured bindings and that seems worth demonstrating somewhere.

  2. Dan Bonachea

    This issue was discussed in the 2021-04-21 Pagoda meeting.

    The consensus was we should start some documentation that collects and demonstrates useful C++17/C++20 idioms as applied to UPC++ types, functions and common operations. This could eventually become a section in the Programming Guide, but for now we can develop it as a page for the wiki, as a pull request against this repo

    Colin was nominated to work on this documentation.

  3. Paul Hargrove

    Please let me know if you do not have the necessary access to the wiki repo Dan mentions in the previous comment.

  4. Log in to comment