Question to FAQ 35.12

A

Arne Claus

Hi.
I ran into the problem described here
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12, but the
solutions presented there only helped to fix a part of my problems.

I use the following construct

template <class T> class A {
public:
typedef smart_ptr<A<T> > aPtr;
virtual void myFunc(aPtr input)=0;
};

template <class T> class B : public A<T> {
public:
virtual void myFunc(aPtr input) { // <- compiler error
// ... do sth. ...
}
};

I tried A<T>::aPtr and using A<T>::aPtr but that doesn't work ...
I also *really* would like a template typedef outside class A, but the
only articles I found on that was one on MSN (I use gcc, so no help
here) and one from 2002 which states, that this would be a feature of
an upcoming C++ standard ... well ... if that standard exist my gcc
version does not seem to use it :(

Arne
 
J

John Carson

Arne Claus said:
Hi.
I ran into the problem described here
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12, but
the solutions presented there only helped to fix a part of my
problems.
I use the following construct

template <class T> class A {
public:
typedef smart_ptr<A<T> > aPtr;
virtual void myFunc(aPtr input)=0;
};

template <class T> class B : public A<T> {
public:
virtual void myFunc(aPtr input) { // <- compiler error
// ... do sth. ...
}
};

I tried A<T>::aPtr and using A<T>::aPtr but that doesn't work ...
I also *really* would like a template typedef outside class A, but the
only articles I found on that was one on MSN (I use gcc, so no help
here) and one from 2002 which states, that this would be a feature of
an upcoming C++ standard ... well ... if that standard exist my gcc
version does not seem to use it :(

Arne

Try either of these (both work with Comeau):

1. Repeat the typedef in the derived class.
2. Rather than A<T>::aPtr, make it typename A<T>::aPtr. With a couple of
exceptions, qualified names (using :: or this->) that depend on a template
parameter will not be treated as typenames unless you use the typename
keyword.
 
L

Larry I Smith

John said:
Try either of these (both work with Comeau):

1. Repeat the typedef in the derived class.
2. Rather than A<T>::aPtr, make it typename A<T>::aPtr. With a couple of
exceptions, qualified names (using :: or this->) that depend on a
template parameter will not be treated as typenames unless you use the
typename keyword.

Hmm, I get an error from g++ v3.3.5:

Line 11 is: typedef smart_ptr<A<T> > aPtr;

arne.cpp:11: error: ISO C++ forbids declaration of `smart_ptr'
with no type
arne.cpp:11: error: template-id `smart_ptr<A<T> >' used as a
declarator


If I replace smart_ptr with auto_ptr, I still get the error.

Regards,
Larry
 
J

John Carson

Larry I Smith said:
Hmm, I get an error from g++ v3.3.5:

Line 11 is: typedef smart_ptr<A<T> > aPtr;

arne.cpp:11: error: ISO C++ forbids declaration of `smart_ptr'
with no type
arne.cpp:11: error: template-id `smart_ptr<A<T> >' used as a
declarator


If I replace smart_ptr with auto_ptr, I still get the error.


I am not familiar with g++ or its error messages. As noted, my suggested
solution works with Comeau. It also works with VC++ 2005 (beta 2).
Naturally, I had to define a smart_ptr class, since none was supplied by the
OP. This was simply

template<class X>
class smart_ptr
{};
 
G

Greg Comeau

Hmm, I get an error from g++ v3.3.5:

Line 11 is: typedef smart_ptr<A<T> > aPtr;

arne.cpp:11: error: ISO C++ forbids declaration of `smart_ptr'
with no type
arne.cpp:11: error: template-id `smart_ptr<A<T> >' used as a
declarator


If I replace smart_ptr with auto_ptr, I still get the error.


You didn't post the revised code that gave that error, but these
should help:


http://www.comeaucomputing.com/techtalk/templates/#whymembernotfound

http://www.comeaucomputing.com/techtalk/templates/#typename


So, for instance, among other choices, this should work:


template <class T> class smart_ptr { };

template <class T> class A {
public:
typedef smart_ptr<A<T> > aPtr;
virtual void myFunc(aPtr input)=0;
};

template <class T> class B : public A<T> {
using typename A<T>::aPtr; // **********************************
public:
virtual void myFunc(aPtr input) { // <- compiler error
// ... do sth. ...
}
};
 
G

Greg Comeau

You didn't post the revised code that gave that error, but these
should help:


http://www.comeaucomputing.com/techtalk/templates/#whymembernotfound

http://www.comeaucomputing.com/techtalk/templates/#typename


So, for instance, among other choices, this should work:


template <class T> class smart_ptr { };

template <class T> class A {
public:
typedef smart_ptr<A<T> > aPtr;
virtual void myFunc(aPtr input)=0;
};

template <class T> class B : public A<T> {
using typename A<T>::aPtr; // **********************************
public:
virtual void myFunc(aPtr input) { // <- compiler error
// ... do sth. ...
}
};

This should be considered too, though it does not apply
to the example under discussion:

http://www.comeaucomputing.com/techtalk/templates/#esprob
 

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

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top