c++ - Passing std::forward_as_tuple() result to multiple functions that may move from that object's rvalue-reference members? -
c++ - Passing std::forward_as_tuple() result to multiple functions that may move from that object's rvalue-reference members? -
edit: think utilize case i'm asking about, when creating function receives tuple of rvalue-references std::forward_as_tuple()
.
the reason question came mind because checking members of objects passed constructor initializers see if rvalue-references (i'm open advice telling me wrong wrong wrong... followed rule of thumb avoid in future, that's prompted question). occurred me that, in different context, might end handing object has rvalue-reference members multiple functions (or function objects), may or may not control, may moves members.
template<typename... args> void my_func(std::tuple<args...>&& tup) { //if tup's members rvalue references, //and function moves guts tup members, then... func_i_dont_control(tup); //what happens here if moves done on same members? another_func_i_dont_control(std::move(tup)); }
i've looked @ use of rvalue reference members?, along other discussions of rvalue reference members, i'm not quite able definitively sort out.
i'm not asking happen, whether scenario should/could happen @ all, , key rules maintain in mind when passing around objects containing rvalue-reference members.
in code func_i_dont_control
cannot silently steal parameter. rvalues bind rvalue references , named variable not rvalue. code either fails compile, or func_i_dont_control
(has overload that) doesn't utilize move semantics.
to give func_i_dont_control
chance steal tuple (to bind tuple rvalue reference), have explicitly cast rvalue std::move(tup)
.
(a function taking lvalue reference should not move it, otherwise can't tell happen.)
edit question doesn't seem tuple members. again, members won't rvalues, func_i_dont_control
need move them explicitly. don't think has "moral right"* so, unless receives whole tuple rvalue, not happening in function.
* move-semantics have follow guidelines. can cast rvalue , move it, doesn't matter if dealing lvalue or rvalue references. long follow these guidelines, move-semantics work correctly. if start casting things rvalues disregarding guidelines, objects start disappearing after function calls etc.
c++ c++11 rvalue-reference temporary lifetime
Comments
Post a Comment