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?
It's a specialization. template<> means that the specialization itself is not templated- i.e., it is an explicit specialization, not a partial specialization.
A class template is a template that is used to generate classes whereas a template class is a class that is produced by a template.
Technical overview. There are three kinds of templates: function templates, class templates and, since C++14, variable templates. Since C++11, templates may be either variadic or non-variadic; in earlier versions of C++ they are always non-variadic.
A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With