Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit instantiation of function template using incomplete type

Tags:

c++

templates

The following:

template< typename >
struct S;

template< typename T >
S< T >& f (S< T >& s) {
    const typename S< T >::nested ignore;
    return s;
}

template S< char >& f (S< char >&);

template< typename >
struct S {
    struct nested { };
};

compiles with gcc, but not with clang:

$ clang -c /tmp/t.cpp
/tmp/t.cpp:6:20: error: implicit instantiation of undefined template 'S<char>'
    const typename S< T >::nested ignore;
                   ^
/tmp/t.cpp:10:21: note: in instantiation of function template specialization 'f<char>' requested here
template S< char >& f (S< char >&);
                    ^
/tmp/t.cpp:2:8: note: template is declared here
struct S;
       ^
1 error generated.

I believe clang to be right in that, at the point of instantiation, the function f refers an incomplete definition of S. OTOH, a later specialization of S might provide the correct definition that makes the dependent 'nested' well-formed. Any opinions?

like image 448
Liviu Nicoara Avatar asked Sep 13 '15 23:09

Liviu Nicoara


1 Answers

Both compilers are correct.

[temp.point]/p6, 8:

6 An explicit instantiation definition is an instantiation point for the specialization or specializations specified by the explicit instantiation.

8 A specialization for a function template [...] may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. [...] If two different points of instantiation give a template specialization different meanings according to the one definition rule (3.2), the program is ill-formed, no diagnostic required.

There are two points of instantiation for f<char>: at the explicit instantiation definition, and at the end of the TU. Because those two points of instantiation would result in different meanings (as lookup for S<T>::nested would yield different results), the program is ill-formed NDR.

like image 88
T.C. Avatar answered Oct 19 '22 07:10

T.C.