문제

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?

도움이 되었습니까?

해결책

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]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top