变量模板和类型推导的模板特化 [英] 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
),所以推导出模板参数的类型从函数返回类型,但那东西对变量模板不起作用(它会导致编译器错误).这是所有已测试编译器(clang、gcc)中的正确行为还是错误?
There's a specialization of a fnc
function declared without an explicit type indication (such as int fnc<int>()
), so the type of template argument is deduced from the function return type, but that thing does not work for variable templates (it leads to compiler error). Is this a correct behavior or a bug in all compilers a have tested (clang, gcc)?
推荐答案
模板参数只能在函数模板的显式特化中省略.由于你有一个模板变量,你必须添加
部分:
Template arguments can only be omitted in explicit specialization of function templates. Since you have a template variable, you have to add the <int>
part:
template <>
int var<int> = 0;
这篇关于变量模板和类型推导的模板特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文