Is there a difference between universal references and forwarding references?

前端 未结 2 800
你的背包
你的背包 2020-11-28 09:28

An argument to this function will bind to an rvalue reference:

void f(int && i);

However, an argument to this function will bind

2条回答
  •  鱼传尺愫
    2020-11-28 10:21

    Do they mean the same thing?

    Universal reference was a term Scott Meyers coined to describe the concept of taking an rvalue reference to a cv-unqualified template parameter, which can then be deduced as either a value or an lvalue reference.

    At the time the C++ standard didn't have a special term for this, which was an oversight in C++11 and makes it hard to teach. This oversight was remedied by N4164, which added the following definition to [temp.deduct]:

    A forwarding reference is an rvalue reference to a cv-unqualified template parameter. If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

    Hence, the two mean the same thing, and the current C++ standard term is forwarding reference. The paper itself articulates why "forwarding reference" is a better term than "universal reference."

    Is it only a forwarding reference if the function body calls std::forward?

    Nope, what you do with a forwarding reference is irrelevant to the name. The concept forwarding reference simply refers to how the type T is deduced in:

    template  void foo(T&& ); // <== 
    

    It does not need to be subsequently forwarded .

提交回复
热议问题