deducing references to const from rvalue arguments

前端 未结 3 1692
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 19:38

Okay, this may seem like a silly question, but here it goes:

template 
void foo(T& x)
{
}

int main()
{
    foo(42);
    // error in pa         


        
3条回答
  •  天涯浪人
    2020-12-17 20:14

    The problem is that template type deduction has to work out an exact match, and in that particular case, because of the reference in the signature, an exact match requires an lvalue. The value 42, is not an lvalue, but rather an rvalue, and resolving T with const int would not yield a perfect match. Since template type deduction is limited to exact matches, that deduction is not allowed.

    If instead of using a literal you use a non mutable lvalue, then the compiler will deduce the type appropriatedly, as const int will become a perfect match for the argument:

    const int k = 10;
    foo( k );            // foo( const int & ) is a perfect match
    

    Now there is a special rule that enables calling a function that takes a const reference (nonmutable lvalue) with an rvalue, that implies creation of a temporary lvalue which is later bound to the reference, but for that rule to kick in the function has to have that signature before hand, which is why explicitly stating that the type of the template is const int works: foo(42).

提交回复
热议问题