变量模板的模板特化和类型推导
Template specialization of variable template and type deduction
template <class C>
C fnc();
template <>
int fnc(){return 0;}
template <class C>
C var;
template <>
int var = 0; // compile error
int main()
{
}
有一个 fnc
函数的特殊化声明,没有明确的类型指示(例如 int fnc<int>()
),因此模板参数的类型是从函数 return 类型推导出来的,但那东西不适用于变量模板(它会导致编译器错误)。这是所有已测试的编译器(clang、gcc)中的正确行为还是错误?
模板参数只能在函数模板的显式特化中省略。由于您有模板变量,因此必须添加 <int>
部分:
template <>
int var<int> = 0;
template <class C>
C fnc();
template <>
int fnc(){return 0;}
template <class C>
C var;
template <>
int var = 0; // compile error
int main()
{
}
有一个 fnc
函数的特殊化声明,没有明确的类型指示(例如 int fnc<int>()
),因此模板参数的类型是从函数 return 类型推导出来的,但那东西不适用于变量模板(它会导致编译器错误)。这是所有已测试的编译器(clang、gcc)中的正确行为还是错误?
模板参数只能在函数模板的显式特化中省略。由于您有模板变量,因此必须添加 <int>
部分:
template <>
int var<int> = 0;