Template, inheritance, sizeof on incomplete type

T

tni

I'm trying to achieve the following:

template<int size>
class A {
};

class B : public A<sizeof(B)> {
};

This obviously doesn't work. Is there some reasonably clean template
magic to achieve this effect?
 
J

Johannes Schaub (litb)

tni said:
I'm trying to achieve the following:

template<int size>
class A {
};

class B : public A<sizeof(B)> {
};

This obviously doesn't work. Is there some reasonably clean template
magic to achieve this effect?

It depends where you need "size". You may delay the sizeof application

template<typename D>
class A {
void f() { ... sizeof(D); ... }
};

Doing it that way is valid if "f" is instantiated at a point where "B" is
complete.
 

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,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top