Forwarding references for non-reference types

前端 未结 2 1492
梦如初夏
梦如初夏 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:12

    Forwarding references deduce lvalue reference for lvalues and rvalue reference for rvalues. For example, even for lvalue of type int&& it will still deduce int& (as it would for lvalue of type int), likewise, for rvalues of type int or int&& it will deduce int&&:

    template<typename T>
    class show;
    
    template<typename T>
    void F(T&& x) 
    { show<decltype(x)>();}
    
    int main() {
        int&& x = 5;
        F(x);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show<int &>'
    
    int main() {
        F(5);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show<int &&>'
    
    int main() {
        int x = 5;
        F(x);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show<int &>'
    
    int main() {
        F([]()->int{return 5;}());
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show<int &&>'
    
    0 讨论(0)
  • 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[...]

    0 讨论(0)
提交回复
热议问题