c++ - Right reference on lvalue -
when rvalue become right reference on lvalue?
example following, , should solved std::forward
:
void g(int && v1, int & v2) { std::cout << v1 << " " << ++v2 << std::endl; } template <typename f, typename t1, typename t2> void flip(f f,t1 && t1, t2 && t2){ g(t2, t1); }
error occurs on call this:
flip(g, j, 2);
my problem is: in flip(g, j 2)
, third argument 2
rvalue.
in function flip
, argument t2
right reference on 2. when g
called, seems t2
right reference lvalue.
in point did rvalue, received reference (without copying?), became lvalue?
here example: https://ideone.com/cjvfcg
there's "if has name" rule saying within code this, if variable has name (in case, t2
) @ point becomes lvalue. (see scott meyers effective modern c++.)
to preserve original intent of caller, indeed use perfect forwarding:
g(std::forward<t2>(t2), ...)
Comments
Post a Comment