“模板"与“模板"没有括号 - 有什么区别?

2021-12-13 00:00:00 templates c++ template-specialization

假设我已经声明:

template <typename T> void foo(T& t);

现在,

template <> void foo<int>(int& t);

template void foo<int>(int& t);

语义上?不带括号的模板和带空括号的模板在其他上下文中是否有其他语义?

semantically? And do template-with-no-brackets and template-with-empty-brackets have other semantics in other contexts?

相关:如何强制实例化 C++ 模板的特定实例?

推荐答案

template <>void foo<int>(int& t); 声明模板的特殊化,可能具有不同的主体.

template <> void foo<int>(int& t); declares a specialization of the template, with potentially different body.

template void foo(int& t); 导致模板的显式实例化,但不引入特化.它只是强制为特定类型实例化模板.

template void foo<int>(int& t); causes an explicit instantiation of the template, but doesn't introduce a specialization. It just forces the instantiation of the template for a specific type.

相关文章