What is an rvalue reference to function type?

后端 未结 2 1396
野的像风
野的像风 2020-12-28 08:45

I have recently wrapped my mind around the C++0x\'s concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there\'s one thing which still elu

2条回答
  •  清酒与你
    2020-12-28 09:42

    In the old c++ standard the following is forbidden:

    int foo();
    void bar(int& value);
    
    int main()
    {
        bar(foo());
    }
    

    because the return type of foo() is an rvalue and is passed by reference to bar().

    This was allowed though with Microsoft extensions enabled in visual c++ since (i think) 2005.

    Possible workarounds without c++0x (or msvc) would be declaring

    void bar(const int& value); 
    

    or using a temp-variable, storing the return-value of foo() and passing the variable (as reference) to bar():

    int main()
    {
        int temp = foo();
        bar(temp);
    }
    

提交回复
热议问题