template and typedef

B

Berardino la Torre

Hi All,

is it possible to do something like this ? :

template <typename Base>
class Object : public Base::Interface {
};

class IA{
virtual void print()=0;
};

class A : public Object<A>{
public:
typedef IA A::Interface;
void print(){
}
};


int main(){

return 0;
}

thx

Bera
 
V

Victor Bazarov

Berardino said:
is it possible to do something like this ? :

template <typename Base>
class Object : public Base::Interface {
};

class IA{
virtual void print()=0;
};

class A : public Object<A>{
public:
typedef IA A::Interface;
void print(){
}
};


int main(){

return 0;
}

What is the intent here? I have hard time figuring out what you
are trying to accomplish. Perhaps if you would show the _use_ for
class A or class IA...

V
 
K

Kira Yamato

Hi All,

is it possible to do something like this ? :

template <typename Base>
class Object : public Base::Interface {
};

g++ seems to complain that you cannot inherit from an incomplete type Base.

This is interesting. Just when you think you've figured out how
template works, here comes another example showing you how much you
don't know.
class IA{
virtual void print()=0;
};

class A : public Object<A>{
public:
typedef IA A::Interface;
void print(){
}
};

Seems like your goal is to have class A inherit from A::Interface. So,
why not consider the following:

class IA {};

class A : public IA
{
public:
typedef IA Interface;
};
 
K

Kira Yamato

g++ seems to complain that you cannot inherit from an incomplete type Base.

This is interesting. Just when you think you've figured out how
template works, here comes another example showing you how much you
don't know.

Well I suppose it makes sense that this should be an error. It's
basically a circular definition that could lead to infinite regress.
For example,

template<class T>
class A : public T {};

class B : public A<B>
{
int x;
};

But this code expands to

class B : public B
{
int x;
};

Hence the infinite regress.
 
J

Jeff Schwab

Berardino said:
Hi All,

is it possible to do something like this ? :

[Summarized for brevity:]

template <typename Base>
struct Object: Base::Interface { };

struct IA { virtual void print() =0; };

struct A: Object<A> {
typedef IA A::Interface;
void print() { }
};


You're trying to derive a class (indirectly) from a type defined within
that class. I don't know of any direct way to do that. You can instead
move the type definition outside the class itself, e.g. into a traits class:

template<typename T>
struct Interface;

template <typename Base>
struct Object: Interface<Base>::Type { };

struct IA { virtual void print() =0; };

struct A;

template<>
struct Interface<A> {
typedef IA Type;
};

struct A: Object<A> {
void print() { }
};
 
J

James Kanze

[Berardino's original post isn't showing up here, but...]
[Summarized for brevity:]
template <typename Base>
struct Object: Base::Interface { };

This line isn't legal C++. What it almost certainly should be
is:

struct Object : typename Base::Interface {} ;

struct IA { virtual void print() =0; };
struct A: Object<A> {
typedef IA A::Interface;
void print() { }
};
You're trying to derive a class (indirectly) from a type
defined within that class.

Which is impossible, templates or not.
 
G

gpderetta

[Berardino's original post isn't showing up here, but...]
[Summarized for brevity:]
template <typename Base>
struct Object: Base::Interface { };

This line isn't legal C++.

This is actually legal and correct. In that context you can only have
a typename,
so you do not need to add the 'typename' keyword. There is no
ambiguity.
What it almost certainly should be
is:

struct Object : typename Base::Interface {} ;

And this is not. Both gcc and comeau online complain that typename is
not allowed in this context.

As if the rules weren't complicated enough... :).
IIRC the next standard will relax the rule
and allow (but not require) a typename there.

HTH,
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top