"template<>" vs "template" 不带括号 - 有什么区别?

"template<>" vs "template" without brackets - what's the difference?

本文关键字:template 什么 区别 lt gt vs      更新时间:2023-10-16

假设我已经声明:

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

现在,有什么区别

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

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

语义上?没有括号的模板和空括号的模板在其他上下文中有其他语义吗?


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

template <> void foo<int>(int& t);声明了模板的专门化,可能具有不同的主体。

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

使用类/结构,

template <typename T> struct foo {};

以下是专业化:

template <> struct foo<int>{};

以下是一个显式实例化:

template struct foo<int>;