What is an rvalue reference to function type?

后端 未结 2 1398
野的像风
野的像风 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:27

    I hate to be circular, but an rvalue reference to function type is an rvalue reference to function type. There is such a thing as a function type, e.g. void (). And you can form an rvalue reference to it.

    In terms of the classification system introduced by N3055, it is an xvalue.

    Its uses are rare and obscure, but it is not useless. Consider for example:

    void f() {}
    ...
    auto x = std::ref(f);
    

    x has type:

    std::reference_wrapper
    

    And if you look at the synopsis for reference_wrapper it includes:

    reference_wrapper(T&) noexcept;
    reference_wrapper(T&&) = delete; // do not bind to temporary objects
    

    In this example T is the function type void (). And so the second declaration forms an rvalue reference to function type for the purpose of ensuring that reference_wrapper can't be constructed with an rvalue argument. Not even if T is const.

    If it were not legal to form an rvalue reference to function, then this protection would result in a compile time error even if we did not pass an rvalue T to the constructor.

提交回复
热议问题