Search code examples
c++templatesgeneric-programming

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?


Solution

  • 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