假设我已宣布:
template <typename T> void foo(T& t);
现在,
之间有什么区别template <> void foo<int>(int& t);
和
template void foo<int>(int& t);
语义?模板与无括号和模板与空括号在其他上下文中是否有其他语义?
相关: How do I force a particular instance of a C++ template to instantiate? 子>
答案 0 :(得分:64)
template <> void foo<int>(int& t);
宣布模板的专业化,可能会有不同的身体。
template void foo<int>(int& t);
会导致模板的显式实例化,但不会引入特化。它只是强制模板的实例化为特定类型。
答案 1 :(得分:12)
使用class / struct,
template <typename T> struct foo {};
以下是专业化:
template <> struct foo<int>{};
以下是一个明确的实例化:
template struct foo<int>;