为什么显式专业化和部分专业化之间的语法差异
Why the difference in syntax between explicit and partial specialization?
示例:
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
void A<T, char>::Print() {} // Will produce error
问题:
我知道你必须在上面的代码中定义类模板部分专业化才能工作,我也从标准中知道The members of the class template partial specialization are unrelated to the members of the primary template (§ 14.5.5.3)
。然而,为什么解释专门化和部分专门化之间的语法差异?
不能部分专门化函数模板,只能完全专门化。
第一个实例利用了这样一个事实,即类模板的成员函数本身就是函数模板,因此限制适用于它们。
当您部分专门化类模板时,您就有了一个全新的类模板,您必须重新定义它。
template <typename T>
void A<T, char>::Print() {} // Will produce error
您是:
- 重新定义一个函数(在声明
void Print() {}
时已经定义了它,您可以看到有{}
- 具有与声明不匹配的模板参数列表:
template <typename T, typename U> void Print()
事实上,即使您在声明函数时没有定义它,您仍然会出现错误,因为您的声明和定义不匹配,编译器将无法找到原始模板的定义或专用模板的声明。
与结构相关的函数的专用模板函数也必须有一个专用结构
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
struct A<T,char>
{
void Print();
};
template <typename T>
void A<T,char>::Print() {}
因为模板函数已在其模板结构中声明。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 如何使用默认参数等选择模板专业化
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 模板化建造师专业化
- 类模板的成员功能的定义在单独的TU中完全专业化
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 部分专业化和嵌套模板
- 模板专业化可以进入我的.cpp吗?
- 单独定义模板化嵌套类方法的正确语法
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 函数模板专业化语法
- 为什么显式专业化和部分专业化之间的语法差异