[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:

x--start--x

class A {};

class B {};

void THIS_IS_AN_ERROR(A& myexp) {
}

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

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

x--end--x

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?

Regards.

-----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>
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.
_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

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