Now I'm not sure what the standard says:
This case was actually clarified with an example in the upcoming C ++ 17
[temp.inst] / 2 Implicitly creating a specialized template specialization ... [snip] ... in order to determine whether an instantiated member revaluation is valid in accordance with 3.2 [basic.def.odr] and 9.2 [class.mem], a definition that matches the definition in the template is considered a definition. [Example:
... [snip (another example)] ...
template<typename T> struct Friendly { template<typename U> friend int f(U) { return sizeof(T); } }; Friendly<char> fc; Friendly<float> ff;
- end of example]
Admittedly, the standard example does create a different definition for each instance, but it is not necessary for this example to be poorly formed in accordance with this rule.
So why can't he apply the "oh, all inline copies of the same function definition are the same" rule?
This question seems to apply to a much simpler situation:
inline void foo(){} inline void foo(){}
Of course, the compiler can see that the definitions are identical, just as the compiler can see that the definition of your ::bar does not depend on the argument of the Foo template.
However, odr says the redefinition is poorly formed. This is true for definitions outside the class template, as well as for definitions caused by the creation of a class template.
Perhaps odr may be weakened for the case you are demonstrating, but this will require complicating the standard with a special case rule and complicating the compilers, which would then need to analyze whether the template arguments are used in the definition, so this relaxation is certainly not without compromise.
user2079303
source share