Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potential evaluation of inline function bodies and instantiation of template members

When are expressions contained inside a function marked inline considered 'potentially evaluated'?

a.cpp

template <typename T>
const T& foo(const T& arg) { return arg; }

inline void dead() {
    int x(21);
    x = foo(x);
}

b.cpp

#include <iostream>

template <typename T> const T& foo(const T&);

int main(int argc, char *argv[]) {
    std::cout << foo(12) << std::endl;
}

If expressions are considered 'potentially evaluated' as soon as the inline function is defined, then the template should be instantiated, and I would expect $(CCC) -c a.cpp; $(CCC) -c b.cpp; $(CCC) a.o b.o -o bin to link successfully. If instead the expressions within a function declared inline only become 'potentially evaluated' when such a function itself becomes odr-used, then I would expect $(CCC) -c a.cpp; $(CCC) -c b.cpp; $(CCC) a.o b.o -o bin to fail during link step.

Thus far I have tested xl C++ 12 (which links successfully), and various versions of gcc + clang 3.5 (all of which fail to link).

Which behaviour is correct? Am I missing a third option here?

like image 641
shroudednight Avatar asked Mar 17 '23 23:03

shroudednight


1 Answers

§14 [temp]/p6:

A function template, member function of a class template, variable template, or static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated (14.7.1) unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required.

Your code is ill-formed with no diagnostic required. Both compilers are behaving correctly.

like image 53
T.C. Avatar answered Mar 25 '23 07:03

T.C.