Why do primitive and user-defined types act differently when returned as 'const' from a function?

前端 未结 4 1560
无人共我
无人共我 2021-02-02 06:53
#include 

using namespace std;

template
void f(T&&) { cout << \"f(T&&)\" << endl; }

template

        
4条回答
  •  忘了有多久
    2021-02-02 07:13

    I don't have a quote from the standard, but cppreference confirms my suspicions:

    A non-class non-array prvalue cannot be cv-qualified. (Note: a function call or cast expression may result in a prvalue of non-class cv-qualified type, but the cv-qualifier is immediately stripped out.)

    The returned const int is just a normal int prvalue, and makes the non-const overload a better match than the const one.

提交回复
热议问题