Forwarding references for non-reference types

前端 未结 2 1493
梦如初夏
梦如初夏 2021-01-25 23:28
 template
 void F(T&& x) {}

If we call it with argument of type int& everything is clear - reference collapsing takes pla

2条回答
  •  失恋的感觉
    2021-01-26 00:14

    Since C++17, there is the concept of forwarding reference in the C++ standard. Normaly the template parameter is deduced as a non reference. But for the specific case of a forwarding reference if the corresponding argument is a lvalue, the parameter is deduced as a reference. C++ standard (N4700)[temp.over.deduct.call]/3:

    [...]A forwarding reference is an rvalue reference to a cv-unqualified template parameter that does not represent a template parameter of a class template (during class template argument deduction ([over.match.class.deduct])). 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.[...]

    For the concern of function call, it has the same meaning as this equivalent paragraph in the C++11(N337) and C++14(N414) [temp.over.deduct.call]/3:

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

提交回复
热议问题