Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c++ - Explicit instantiation of function template using incomplete type

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...