[cfe-dev] Template error without instantiation?

Handa, Ramneek Ramneek.Handa at sc.com
Thu Apr 25 00:58:35 PDT 2013

Thanks John. 

Making small immaterial adjustments to code:


class A {};

class B {};

void THIS_IS_AN_ERROR(A& myexp) {

template <typename T>
void mytest(const B &value)

template <typename T>
void mytest(const T &value) 
   //Do something useful, ignore what we had here earlier


It's a case of template specialization where I would like the compiler
to throw an error if B is being passed to mytest.
If not I would like it to do the *useful thing*.

Could you please point me to where in the standard document does it say
that compiler can reject a template if it has no valid instantiation,
without actually being asked to instantiate it?


-----Original Message-----
From: cfe-dev-bounces at cs.uiuc.edu [mailto:cfe-dev-bounces at cs.uiuc.edu]
On Behalf Of John McCall
Sent: Thursday, April 25, 2013 2:17 PM
To: Ramneek Handa
Cc: clang-dev Developers
Subject: Re: [cfe-dev] Template error without instantiation?

On Apr 24, 2013, at 8:02 AM, Ramneek Handa <ramneekhanda at gmail.com>
>>> 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
>>> 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
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

Clang is somewhat more aggressive than GCC about diagnosing errors
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.

cfe-dev mailing list
cfe-dev at cs.uiuc.edu

This email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please delete all copies and notify the sender immediately. You may wish to refer to the incorporation details of Standard Chartered PLC, Standard Chartered Bank and their subsidiaries at http://www.standardchartered.com/en/incorporation-details.html.

More information about the cfe-dev mailing list