gcc at fault?

W

werasm

Hi all,

This code failed to compile (using GCC 4.1). Other compilers did not
complain.

template <class T>
struct Base
{
Base( T& ){}
};

struct Derived : Base<Derived>
{
Derived()
: Base( *this ) <<---Error here!
{}
};

GCC failed with error:

error: no matching function for call to
`appCFDC::Base<appCFDC::Derived>::Base()'

The problem is fixed by qualifying the Base type explicitly:
: Base<Derived>( *this )...

Is GCC at fault here? Anybody willing to sight the standard?

Kind regards,

Werner
 
V

Victor Bazarov

werasm said:
This code failed to compile (using GCC 4.1). Other compilers did not
complain.

template <class T>
struct Base
{
Base( T& ){}
};

struct Derived : Base<Derived>
{
Derived()
: Base( *this ) <<---Error here!
{}
};

GCC failed with error:

error: no matching function for call to
`appCFDC::Base<appCFDC::Derived>::Base()'

The problem is fixed by qualifying the Base type explicitly:

Is GCC at fault here? Anybody willing to sight the standard?

Too little sleep last night to be able to dig through the Standard,
but GCC's behaviour does look justified. 'Base' is a template-id,
not a type-id. To make it a type-id (which is what should be found
when looking up the base class name), one needs the template
argument list (including the angle brackets)...

I am sure somebody will find that (or a rebuttal to that) in the
Standard.

V
 
W

werasm

Victor said:
Too little sleep last night to be able to dig through the Standard,
but GCC's behaviour does look justified.

Yes, I have the same problem. Too little time to delve into
the standard (need to keep that balance...). I also know there
are guys here that know the standard by heart (almost) and
probably know the answer, hence the question. I also prefer
writing code that is compliant (else I run into trouble with code
that fails to compile somewhere down the line).

Explicitly qualifying Base will never be wrong, therefore it is
probably the way to go (if the other way cannot be proven right,
at least). Was just curious.

Regards and Thanks,

Werner
 
J

Joe Greer

Yes, I have the same problem. Too little time to delve into
the standard (need to keep that balance...). I also know there
are guys here that know the standard by heart (almost) and
probably know the answer, hence the question. I also prefer
writing code that is compliant (else I run into trouble with code
that fails to compile somewhere down the line).

Explicitly qualifying Base will never be wrong, therefore it is
probably the way to go (if the other way cannot be proven right,
at least). Was just curious.

Regards and Thanks,

Werner

Well, *I* know that VC doesn't use correct two phase lookup and
therefore finds names it technically shouldn't. I have never had a case
where this caused actual problems though, but technically it's wrong.

If I had to guess, I would guess gcc is right as well. My reasoning
(and it may well be faulty) goes like this. What is needed to invoke
the base class constructor is a class name. Template classes can't
deduce their template argument types, therefore you need to provide it.
Same rule holds here, Base would have to somehow deduce its template
argument and that doesn't normally work.

As you can see though, I have no rules to quote and my quick attempt at
finding them proved that I am not proficient in standard-ness.

joe
 
M

Markus Schoder

Hi all,

This code failed to compile (using GCC 4.1). Other compilers did not
complain.

template <class T>
struct Base
{
Base( T& ){}
};

struct Derived : Base<Derived>
{
Derived()
: Base( *this ) <<---Error here!
{}
};

GCC failed with error:

error: no matching function for call to
`appCFDC::Base<appCFDC::Derived>::Base()'

The problem is fixed by qualifying the Base type explicitly: :
Base<Derived>( *this )...

Is GCC at fault here? Anybody willing to sight the standard?

GCC is correct. By 12.6.2/2 in the above case the name of a class needs
to be given. The name of a class according to 9/1 is either the name of a
non-template class/struct/union or a template-id. Here it obviously needs
to be a template-id which includes the template argument list in <>
brackets according to 14.2/1.

The above code is therefore indeed ill-formed. Compilers not outputting a
diagnostic for the above code do not conform to the standard (which of
course most compilers do not anyway for various reasons).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top