Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 792
einpoklum Avatar asked Feb 05 '15 21:02

einpoklum


People also ask

What does template <> mean in C++?

It's a specialization. template<> means that the specialization itself is not templated- i.e., it is an explicit specialization, not a partial specialization.

What is the difference between class template and template class?

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.

What is template How many types of template are there in?

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.

What is template and its types?

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.


1 Answers

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.

like image 132
Mark B Avatar answered Sep 23 '22 23:09

Mark B