gpt4 book ai didi

C++ 模板选择

转载 作者:可可西里 更新时间:2023-11-01 15:02:23 24 4
gpt4 key购买 nike

给定以下代码:

#include <memory>
#include <iostream>

using namespace std;

template<typename T>
void test(T & value) {
cout << "most generic" << endl;
}

template<typename T>
void test(shared_ptr<T> & value) {
cout << "shared_ptr" << endl;
}

class A {};

int main(int argc, char ** argv) {
A a;
shared_ptr<A> p(new A());
test(a);
test(p);
return 0;
}

为什么叫

test(p)

用 T = A 实例化第二种形式的测试,而不是提示它无法区分两个签名?

最佳答案

因为尽管它们都是重载解析的可行选择,但第二个函数模板比​​第一个更专业

根据 C++11 标准的第 13.3.3/1 段:

[...] Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

— the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. [...] or, if not that,

— F1 is a non-template function and F2 is a function template specialization, or, if not that,

— F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.

§ 14.5.6.2 然后说明了如何确定一个函数模板比​​另一个函数模板更专业。特别是,根据 14.5.6.2/2:

Partial ordering selects which of two function templates is more specialized than the other by transforming each template in turn (see next paragraph) and performing template argument deduction using the function type. The deduction process determines whether one of the templates is more specialized than the other. If so, the more specialized template is the one chosen by the partial ordering process.

标准中的正式定义可能很难破译,但它们的复杂性通常意味着在大多数情况下明确地使语言表现得像我们自然期望的那样

我们对您提供的重载的直觉期望是接受 std::shared_ptr<T> 的重载当参数类型为 std::shared_ptr<int> 时应选择,因为它似乎正在处理 std::shared_ptr<>特定对象,因此,它看起来比不受约束的重载更好(更专业)。

将这种直觉期望转化为明确的规则集的正式过程可能听起来很复杂,但在我们的情况下遵循它并不是特别困难,我们要确定这种重载是否:

template<typename T>
void f(std::shared_ptr<T>);

比这个更专业:

template<typename U>
void f(U);

虽然在这种情况下,我们很容易根据直觉判断哪个更专业,但编译器必须依赖于算法,并且该算法必须适用于所有情况。

在这种情况下,机制将如下所示:

  1. 采用第一个 重载,替换其模板参数T对于类型参数(任何 类型参数),例如 int ,并实例化相应的签名 - 函数参数的类型为 std::shared_ptr<int> ;
  2. 是否总是可以通过提供该类型的对象(在本例中为 shared_ptr<int>)作为其输入来调用第二个 重载,并推导出类型 U从它?
  3. 嗯,答案是是的U将被推断为 std::shared_ptr<int> ;
  4. 现在反过来:采用第二个重载,替换它的模板参数U对于任何类型的参数,例如 bool ,并实例化相应的签名 - 函数参数的类型为 bool ;
  5. 是否总是可以通过提供该类型的对象 ( bool ) 作为其参数来调用 first 重载并推导出类型 T从它?
  6. 当然,这里的答案是。无法推断T这样std::shared_ptr<T>会匹配bool ;
  7. 结论:第一个重载比第二个重载更专业

当然,当有多个模板参数和多个函数参数时,事情会稍微复杂一些,但机制几乎相同。

关于C++ 模板选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15497004/

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