Yakov Galka
Yakov Galka

Reputation: 72489

ODR violation when name-lookup finds a different declaration

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?

Upvotes: 5

Views: 444

Answers (1)

CB Bailey
CB Bailey

Reputation: 792009

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]

Upvotes: 7

Related Questions