[cfe-dev] Template error without instantiation?

John McCall rjmccall at apple.com
Wed Apr 24 23:17:25 PDT 2013


On Apr 24, 2013, at 8:02 AM, Ramneek Handa <ramneekhanda at gmail.com> wrote:
>>> Hey folks,
>>> Can you help check which compiler behaves correctly here? To my
>>> untrained mind it seems to be a clang issue but seems too basic to have
>>> been missed unintentionally?
>>> 
>>> Regards,
>>> Ramneek
>>> 
>>> x- start -x
>>> class A {};
>>> 
>>> class B {};
>>> 
>>> void myfun(A& myexp) {
>>> }
>>> 
>>> template <typename T>
>>> void mytest(const B &value)
>>> {
>>> myfun(A(value));
>>> }

The behavior of both compilers is consistent with the standard.

This template has no valid instantiation;  there is no type T which will allow
an A to be constructed from a const l-value of type B.  A template with no valid
instantiation is ill-formed with no diagnostic required (unless instantiated, of
course).

Clang is somewhat more aggressive than GCC about diagnosing errors within
templates at definition time rather than at instantiation time.

>>> template <typename T>
>>> void mytest(const T &value)
>>> {
>>> myfun(A(value));
>>> }

This template does have a valid instantiation:  for example, T=A.  Therefore it
is not ill-formed.

John.



More information about the cfe-dev mailing list