Why are rvalues references variables not rvalue?

前端 未结 3 1432
我在风中等你
我在风中等你 2020-11-27 21:23

Let\'s say I have two overloads of a function f. f(T&&) and f(T&). Then in the body of g: g(T&&

3条回答
  •  星月不相逢
    2020-11-27 21:40

    In g() t is a named variable. All named variables are lvalues. If T is a template type then you can forward the variable to f() using std::forward. This will call f() with the same type as was passed to g()

    template
    g(T&& t) { f(std::forward(t));}
    

    If T is not a template type but just a type then you can use std::move

    g(T&& t) { f(std:move(t)); }
    

提交回复
热议问题