Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The order of template specializations in C++

Can the order in which template specializations appear in the code alter the meaning of the program? If so, then why?

Example:

Somewhere inside a source code

// specialization A
...
// specialization B
...

vs.

// specialization B
...
// specialization A
...

Will this always produce the same result?

like image 288
user6646922 Avatar asked Apr 23 '18 13:04

user6646922


People also ask

What is a template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

Are template specializations inline?

An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.

How many types of template are there?

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.

How many types of specialization are there in C++?

How many types of specialization are there in c++? Explanation: There are two types of specialization. They are full specialization and partial specialization.


2 Answers

The placement of explicit specialization declarations for function templates, class templates, member functions of class templates, static data members of class templates, member classes of class templates, member class templates of class templates, member function templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, member class templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

Simply quoting section 14.7.3/7 of the 2011 International Standard for Programming Language C++

Yes... this is not a Joke

like image 121
Stefano Avatar answered Sep 28 '22 21:09

Stefano


So long as a snippet of code that relies on the specialisations has already seen them, the order does not matter.

In other words, the order would matter with

// specialization A
// Some code where B would be a better match
// specialization B
like image 22
Bathsheba Avatar answered Sep 26 '22 21:09

Bathsheba