部分模板专门化歧义
Partial template specialization ambiguity
我不明白为什么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>
匹配第二个但不匹配第一个。
因此没有一个比另一个更专门化,匹配两个专门化的模板实例化将无法编译。
相关文章:
- 构造对象的歧义
- 是否可以对零模板参数进行模板专门化
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 数组初始值设定项的构造函数歧义
- 消除好友和成员二进制运算符的歧义
- 用' . '代替' :: '会在C++中造成歧义吗?
- 为什么下面带有非常量转换函数的代码没有歧义?
- 如何为指向复杂值的迭代器专门化算法?
- 专门化模板覆盖函数/避免对象切片
- 我能否根据其运算符()的签名专门化可变参数模板参数
- std::bind 是否实现了 std::ref 和 std::cref 来消除函数调用的歧义?
- 如何使用模板化类专门化模板化函数?
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- 线程 std::调用未知类型,无法专门化函数错误
- 如何使用is_base_of专门化模板,而不会与主模板产生歧义
- 可变类型的部分模板专门化和扩展到外部类型的可变参数包会导致歧义
- 部分模板专门化歧义
- c++模板元编程专门化歧义