Home > Software design >  Are value categories preserved inside a tuple (perfect forwarding through tuples)?
Are value categories preserved inside a tuple (perfect forwarding through tuples)?

Time:01-21

I wrote the following snippet to test if I could perfectly forward values through a tuple and std::invoke. However the generated assembly looks kind of odd.

auto async(Cb&& fn, Args&&... args) { constexpr std::size_t cnt = sizeof...(Args); auto tuple = std::make_tuple(std::forward(args)...); std::invoke([&](std::index_sequence){ foo(std::get(tuple)...); }, std::make_index_sequence{}); } int main() { async(&foo, std::string("mystring"), std::string("other")); }'),l:'5',n:'0',o:'C++ source #1',t:'0')),k:57.59791122715404,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:gsnapshot,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c++,libs:!(),options:'-Wall -Os --std=c++20',selection:(endColumn:30,endLineNumber:43,positionColumn:30,positionLineNumber:43,selectionStartColumn:8,selectionStartLineNumber:30,startColumn:8,startLineNumber:30),source:1),l:'5',n:'0',o:' x86-64 gcc (trunk) (Editor #1)',t:'0')),header:(),l:'4',m:73.65470852017937,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64 gcc 12.2',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output of x86-64 gcc (trunk) (Compiler #1)',t:'0')),k:50,l:'4',m:26.34529147982063,n:'0',o:'',s:0,t:'0')),k:42.40208877284596,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4" rel="nofollow noreferrer">Demo

#include <concepts>
#include <string>
#include <cstdio>
#include <tuple>
#include <functional>


auto foo(std::string str, std::string str2 = "temp")
{
    printf("Foo executed with %s and %s!\n", str.data(), str2.data());
}


template <typename Cb, typename... Args>
auto async(Cb&& fn, Args&&... args)
{
    constexpr std::size_t cnt = sizeof...(Args);
    auto tuple = std::make_tuple<Args...>(std::forward<Args>(args)...);

    std::invoke([&]<std::size_t... I>(std::index_sequence<I...>){
        foo(std::get<I>(tuple)...);
    }, std::make_index_sequence<cnt>{});
}


int main()
{
    async(&foo, std::string("mystring"), std::string("other"));
}

x86-46 gcc 12.2 -Wall -Os --std=c 20

Excerpt from line 30:

        call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) [complete object constructor]
        lea     rsi, [rsp 16]
        lea     rdi, [rsp 176]
        call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) [complete object constructor]
        lea     rsi, [rsp 144]
        lea     rdi, [rsp 112]
        call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [complete object constructor]
        lea     rsi, [rsp 176]
        lea     rdi, [rsp 80]
        call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [complete object constructor]
        lea     rsi, [rsp 112]
        lea     rdi, [rsp 80]
        call    foo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

As you can see there's a call to the copy constructor of std::string. Where does it originate from? My suspicion is the call to foo() using std::get<I>(tuple) which doesn't seem to perfectly forward the values. In this regard I'm lacking understanding: How is "rvalue-ness" routed through a std::tuple? Is the value category preserved inside the tuple or is everything converted to either copy or lvalue-reference? Is there a chance to optimize this as well?

Clarification:

My goal is essentially to achieve perfect forwarding from the callsite of async() to foo(). That means value categories should be preserved as-is and no unnecessary copies should be made in between.

E.g. When I call async(&foo, std::string("string1"), std::string("string2")) it should invoke the move constructor for the strings constructed in foo's parameter list.

E.g. When I call async(&foo, str1, str2) with predefined strings they should be picked up as rvlaue-references instead and foo's parameters should be copy-constructed.

CodePudding user response:

std::make_tuple makes a new tuple of values of the passed types. The result isn't a tuple storing references at all.

If you want a tuple of references you need std::forward_as_tuple instead. If you save the result in a variable, you are of course responsible to make sure that the referenced objects outlive the result tuple.

Also, neither std::make_tuple nor std::forward_as_tuple are supposed to be given template arguments explicitly. That will break their expected properties. (I guess for std::make_tuple it isn't that strict of a rule, but for std::forward_as_tuple it is.)

// tuple is a `std::tuple` of references of appropriate kind
// matching the value categories of `args`
auto tuple = std::forward_as_tuple(std::forward<Args>(args)...);

Furthermore, std::get will forward the value category only if you pass the tuple as rvalue. This means you need to use std::move explicitly. This prevents that an element referenced by the tuple can be moved without any explicit indication of that in the code:

std::invoke([&]<std::size_t... I>(std::index_sequence<I...>){
    foo(std::get<I>(std::move(tuple))...);
}, std::make_index_sequence<cnt>{});

However, the standard library already has std::apply which does exactly this for you:

std::apply(fn, std::move(tuple));

Also, I assume that you need the tuple for something else as well, because otherwise you can simply do

std::invoke(fn, std::forward<Args>(args)...);

(And you probably want std::forward<Cb>(fn) instead of fn as well in case that is something more complex than a function pointer.)


Also note that using async as name for your function is not a good idea. There is a function template with the same name, template head and function parameter list in the std namespace. As a consequence, whenever your async is called with a type being from or depending on the namespace std, it will be ambiguous with std::async in overload resolution because it is found via ADL.

Therefore users would need to always make sure to call your async only by qualified name (which doesn't cause ADL). Otherwise the user's code may fail to compile randomly (because any standard library might or might not include std::async indirectly) and will certainly break any user that included <future>.

See https://godbolt.org/z/zan7YorYT for a demonstration.

  • Related