Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 697
Yakov Galka Avatar asked May 16 '11 07:05

Yakov Galka


1 Answers

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]

like image 175
CB Bailey Avatar answered Sep 20 '22 06:09

CB Bailey