gpt4 book ai didi

c++ - 转发引用的绑定(bind)优先级

转载 作者:行者123 更新时间:2023-11-30 03:27:52 26 4
gpt4 key购买 nike

谁能帮我理解为什么下面的代码输出 T&& 而不是 const A&:

class A{};

template< typename T >
void foo( T&& )
{
std::cout << "T&&" << std::endl;
}


void foo( const A& )
{
std::cout << "const A&" << std::endl;
}


int main()
{
A a;
foo( a );
}

最佳答案

[temp.deduct.call]/1 & 3 [强调我的]:

/1

Template argument deduction is done by comparing each function template parameter type (call it P) that contains template-parameters that participate in template argument deduction with the type of the corresponding argument of the call (call it A) as described below. ...

/3

... A forwarding reference is an rvalue reference to a cv-unqualified template parameter that does not represent a template parameter of a class template (during class template argument deduction ([over.match.class.deduct])). If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

[ Example:

...
template <class T> int f(T&& heisenreference);
int i;
int n1 = f(i); // calls f<int&>(int&)
...

— end example]

应用于您的示例,foo(a)对于模板参数推导,调用将解析为 void foo<A&>(A&) ,它与 cv-unqualified 左值 a 完全匹配.非模板函数 void foo(const A&) , 将由 [over.ics.rank]/3.2.6 (感谢@M.M 纠正了消除这两个歧义的排名规则),提供比模板扣除一个更糟糕的转换序列,并且 void foo<A&>(A&)将优先于重载决议。

/3.2

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if

  • ...

  • /3.2.6S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.

如果您修改示例,使非模板重载具有与模板相同的顶级 cv 限定符(例如,通过从非模板函数中删除 const cv 限定符) , 它将导致先验被重载决议选择,.

class A {};

template< typename T >
void foo( T&& )
{
std::cout << "T&&" << std::endl;
}

void foo( A& )
{
std::cout << "A&" << std::endl;
}

int main()
{
A a;
foo( a ); // "A&"
}

关于c++ - 转发引用的绑定(bind)优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com