“模板”与“模板”没有括号-有什么区别? [英] "template<>" vs "template" without brackets - what's the difference?
问题描述
假设我已经声明:
template< typename T>无效foo(T& t);
现在,
<$有什么区别p $ p>
模板<> void foo< int>(int& t);
和
template void foo< int>(int& t);
显然吗?在其他情况下,无括号模板和无括号模板还有其他语义吗?
template<> void foo< int>(int& t);
声明模板的专业化,其主体可能不同。
template void foo< int>(int& t);
导致模板的显式实例化,但未引入专门化。只是强制为特定类型实例化模板。
Suppose I've declared:
template <typename T> void foo(T& t);
Now, what is the difference between
template <> void foo<int>(int& t);
and
template void foo<int>(int& t);
semantically? And do template-with-no-brackets and template-with-empty-brackets have other semantics in other contexts?
Related to: How do I force a particular instance of a C++ template to instantiate?
template <> void foo<int>(int& t);
declares a specialization of the template, with potentially different body.
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.
这篇关于“模板”与“模板”没有括号-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!