mercredi 1 avril 2015

Passing reference as lvalue/rvalue depending on function signature

Let's say I have some data :



struct Bar {};


I need to wrap a function and feed it with this data.



template<typename F>
void foo(F f) {
Bar bar;
f(bar);
}


As you can see in this trivial example:



  • bar is not a temporary

  • I don't need it after calling f


I want to support multiple function signatures, such as :



foo([](Bar){}); // (1)
foo([](Bar&){}); // (2)
foo([](Bar&&){}); // (3)


However gcc complains :



f(bar); // (3) : cannot bind 'Bar' lvalue to 'Bar&&'
f(std::move(bar)); // (2) : no match for call to ...


How would you do to get both ?


Aucun commentaire:

Enregistrer un commentaire