Question

I've been thinking of the following. Consider two files:

A.cpp:

template<class T> void g(T) {}

inline void f() { g(1); }

B.cpp:

template<class T> void g(T) {}
void g(int) {}

inline void f() { g(1); }

Without void g(int) {} this program is 100% valid. With void g(int) {}, g(1) resolves to the template version in A.cpp and to the non-template in B.cpp.

Does this program violate ODR? Why?

Was it helpful?

Solution

Yes, it does. In the exception for inline functions it's specified that not only shall the definitions of the inline function consist of exactly the same token sequence but that all the corresponding identifiers in the function definition which name entities outside of the function definition must refer to the same entity (with a few minor exceptions, such as referring to const objects with internal linkage with the same definition being allowed). [see ISO/IEC 14882:2003 3.2/5]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top