Forwarding references for non-reference types

前端 未结 2 1497
梦如初夏
梦如初夏 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
    class show;
    
    template
    void F(T&& x) 
    { show();}
    
    int main() {
        int&& x = 5;
        F(x);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show'
    
    int main() {
        F(5);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show'
    
    int main() {
        int x = 5;
        F(x);
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show'
    
    int main() {
        F([]()->int{return 5;}());
    }
    
    main.cpp:6:3: error: implicit instantiation of undefined template 'show'
    

提交回复
热议问题