18

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?

user6646922
  • 497
  • 1
  • 6
  • 15
  • 2
    No it wont. Think of: static variables whose init has side-effects, dependant definitions, etc. – YSC Apr 23 '18 at 13:10

2 Answers2

36

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

Stefano
  • 3,981
  • 8
  • 36
  • 66
  • 3
    This is [`[temp.expl.spec]/7`](http://eel.is/c++draft/temp.expl.spec#7) (I had to look for it, sorry I didn't blindly trust you on that one). – YSC Apr 23 '18 at 13:17
  • I had also to google it... I just remember that there was something in the standard as i found the description quite funny! – Stefano Apr 23 '18 at 13:22
  • 2
    It's 17.8.3/7 in the C++17 draft. It even has more stuff in it. Turns out language lawyers _do_ have a sense of humour. – isanae Apr 23 '18 at 19:31
  • 3
    Here's a trimmed version of that quote (which I find easier to read): *"The placement of explicit specialization declarations [...] and the placement of partial specialization declarations [...] can affect whether a program is well-formed [...]. 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."* – Cornstalks Apr 23 '18 at 21:58
  • 10
    I love how this passage contains a list of sixteen different varations on the template concept, followed by "etc." – Spencer Apr 23 '18 at 23:15
15

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
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    But note this doesn't necessarily mean something must come between the two specializations. In particular, if "specialization A" is an explicit specialization definition and that definition *contains* "some code where B would be a better match" (and B has not been declared yet), bang, undefined behavior. (This can normally be solved by declaring and then defining the specializations.) – aschepler Apr 23 '18 at 22:50