gpt4 book ai didi

c++ - C++11 标准中的哪条规则描述了下面提到的匹配?

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:30 25 4
gpt4 key购买 nike

std::remove_reference使用以下实现:

template< class T > struct remove_reference      {typedef T type;};
template< class T > struct remove_reference<T&> {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};

因此,如果有人做 std::remove_reference<int&>类型 int&将匹配 T&特化。如果我们使用 std::remove_reference<int&&>类型 int&&匹配T&&特化。

我想知道标准中的哪条规则描述了这个匹配过程。

最佳答案

§14.5.5.1/1

When a class template is used in a context that requires an instantiation of the class, it is necessary to determine whether the instantiation is to be generated using the primary template or one of the partial specializations. This is done by matching the template arguments of the class template specialization with the template argument lists of the partial specializations.

— If exactly one matching specialization is found, the instantiation is generated from that specialization.

— If more than one matching specialization is found, the partial order rules (14.5.5.2) are used to determine whether one of the specializations is more specialized than the others. If none of the specializations is more specialized than all of the other matching specializations, then the use of the class template is ambiguous and the program is ill-formed.

— If no matches are found, the instantiation is generated from the primary template.

匹配的过程类似于函数模板参数推导。 §14.5.5.1/2:

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list (14.8.2). ...

对于 std::remove_reference<int&>只有 T&部分特化匹配,所以它被选中。对于 std::remove_reference<int&&>只有 T&&部分特化匹配,因此被选中。

关于c++ - C++11 标准中的哪条规则描述了下面提到的匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961899/

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