显式模板专门化的语法
Syntax for explicit template specializations
当编译为c++ 98或c++ 11时,gcc-4.9.2和clang-3.8都接受以下内容,
#include <cstdio>
template <typename T> void f(T) { printf("Tn"); }
template <> void f<int>(int) { printf("intn"); } // explicit specialization
template <> void f<>(double) { printf("doublen"); } // explicit specialization -- 14.7.2(7)
template <> void f(float) { printf("floatn"); } // HERE
int main() {
f(1L); // T
f(10); // int
f(10.0); // double
f(10.0F); // float
}
我看到在c++ 11标准§14.7.2(7)中允许在显式模板特化中推导末尾的模板参数,但我找不到标记为HERE
的更简洁的形式是否或如何被允许。
这些编译器是一致的还是这是一些扩展?
14 c++标准§14.7 (3)
可以为函数模板、类模板、类模板的成员或成员模板声明显式特化。一个显式特化声明是由template<>引入的。在类模板、类模板成员或类成员模板的显式特化声明中,被显式特化的类的名称应该是simple-template-id。在函数模板或成员函数模板的显式特化声明中,显式特化的函数或成员函数的名称可以是template-id。
然后演示
template<class U> void g(U) { } template<> void g(char) { } //specialize for U == char // U is deduced from the parameter type
然后我们有§14.7.3(10)
如果可以从函数实参类型推断出,则可以在指定显式函数模板专门化的template-id中不指定后面的模板实参。(例子:
template<class T> class Array { / ... / }; template<class T> void sort(Array<T>& v); // explicit specialization for sort(Array<int>&) // with deduced template-argument of type int template<> void sort(Array<int>&);
-end example]
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 是否可以对零模板参数进行模板专门化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 尝试根据类中 typedef 的存在来专门化模板函数
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- c++模板专门化语法
- 显式模板专门化的语法
- 模板专门化GCC的语法错误,但MSVC没有
- c++模板全专门化语法
- 需要语法帮助来专门化基于模板参数类型的类方法
- c++中专门化函数模板的语法
- 模板专门化语法错误?不确定
- 定义模板方法专门化的正确语法是什么?