gpt4 book ai didi

c++ - 部分模板特化歧义

转载 作者:IT老高 更新时间:2023-10-28 23:19:00 26 4
gpt4 key购买 nike

我不明白为什么 main 中的语句模棱两可。

template<class T, class U, int I> struct X
{ void f() { cout << "Primary template" << endl; } };


template<class T, int I> struct X<T, T*, I>
{void f() { cout << "Partial specialization 1" << endl;}};

template<class T, class U, int I> struct X<T*, U, I>
{void f() { cout << "Partial specialization 2" << endl;}};

template<class T> struct X<int, T*, 10>
{void f() { cout << "Partial specialization 3" << endl;}};

template<class T, class U, int I> struct X<T, U*, I>
{void f() { cout << "Partial specialization 4" << endl;}};

int main()
{
X<int, int*, 10> f;
}

不是X<int, T*, 10>最专业的模板?这是来自 http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fpartial_specialization.htm 的示例

最佳答案

如果与第一个匹配的每个参数列表也匹配第二个,则模板特化比另一种更特化,但反之则不然。

查看 X<int, T*, 10> 时和 X<T, T*, I> :

  • X<int, float*, 10>匹配第一个但不匹配第二个。
  • X<float, float*, 10>匹配第二个但不匹配第一个。

因此,两者都不比另一个更特化,并且匹配这两种特化的模板实例将无法编译。

关于c++ - 部分模板特化歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637145/

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