在 C++ Primer Plus(2001,捷克语翻译)中,我发现了这些不同的模板专业化语法:
函数模板
template <typename T> void foo(T);
专业化语法
void foo(int param); // 1
void foo<int>(int param); // 2
template <> void foo<int>(int param); // 3
template <> void foo(int param); // 4
template void foo(int param); // 5
谷歌搜索了一下,我只找到了第三个例子。它们之间(调用、编译、使用)有什么区别吗?其中一些是否已过时/已弃用?为什么不直接使用No.1呢?
以下是每种语法的注释:
void foo(int param); //not a specialization, it is an overload
void foo<int>(int param); //ill-formed
//this form always works
template <> void foo<int>(int param); //explicit specialization
//same as above, but works only if template argument deduction is possible!
template <> void foo(int param); //explicit specialization
//same as above, but works only if template argument deduction is possible!
template void foo(int param); //explicit instantiation
我添加的:
//Notice <int>. This form always works!
template void foo<int>(int param); //explicit instantiation
//Notice <>. works only if template argument deduction is possible!
template void foo<>(int param); //explicit instantiation
从编码的角度来看,重载优于函数模板专业化。
因此,不要专门化函数模板:
并了解术语:
看看这个:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)