ODR and "same sequence of tokens" and "typename" vs "class" in type template parameters

Viewed 57
  • [Disclaimer #1: I'm actually not referring to the C++ standard; I'm referring to cppreference's "Definitions and ODR (One Definition Rule)".]
  • [Disclaimer #2: I'm actually too lazy to try it, which I defend in my mind by referring to the standard C++ disclaimer that ODR violations are not required to be diagnosed, so I probably won't learn anything by trying it.]

The C++ standard (as disclaimed in #1 above) says that one of the requirements for not being an ODR violation is "each definition consists of the same sequence of tokens".

Which seems pretty clear, except, I'm curious:

In declaring the type template parameters of a template, is substituting typename for class in two different definitions (in two different translation units, etc.) disallowed as well?

Because those keywords are generally interchangeable.

(I know about the special case that you can't substitute typename for class in a template template parameter in any event.)

1 Answers

typename and class are not the same token. So yes, if two definitions for the same entity differ on that, then it is an ODR violation.

The ODR rule is written with the expectation that template/inline definitions are only written out once in a header file which is then included in other files, so that such variations shouldn't happen.

Related