inherits mixin. What is wrong?

V

viboes

Hello,

Very often we need to name the basetype of a derived type

class D : public B {
typedef B base_type;
// ...
};

When B es a complexe template expression we need to repeat the
complete expression, which is inconvenient and error prono.

class D : public 'very complex
template expression' {
typedef 'very complex
template expression' base_type;
// ...
};

The inmediate solution comes with the use of the preprocesor

#define XXX_BASE 'very complex \
template expression'

class D : public XXX_BASE {
typedef XXX_BASE base_type;
#undef XXX_BASE
// ...
};

Even if this helps to increase maintenability, it is a little bit
artificial. I would like something more C++ oriented. I have defined a
mixin class that defines a typedef to the inherited base class as
follows:

template <typename T>
struct inhetits : public T {
typedef T base_type;
};

This works well with non-template classes
struct B {
typedef int ii;
};
struct D : inhetits<B> {
typedef base_type::ii jj;
static jj g(jj v) {return v+1;}
};

But do not compiles when the inherited class is a template

template<typename T>
struct BT {
typedef T ii;
};

template<typename T>
struct DT : inhetits<BT<T> > {
typedef typename base_type::ii jj;
static jj g(jj v) {return v+1;}
};

test.cpp:74: error: `base_type' has not been declared
test.cpp:74: error: expected nested-name-specifier before "ii"
test.cpp:74: error: `ii' does not name a type
test.cpp: In static member function `static jj DT<T>::g(jj)':
test.cpp:75: error: no match for 'operator+' in 'v + 1'
test.cpp: In function `int main()':

Why base_type is not visible? Why this do not works?

Note that the following works:

template<typename T>
struct D2T : inhetits<B> {
typedef base_type::ii jj;
static jj g(jj v) {return v+1;}
};

struct D2 : inhetits<BT<int> > {
typedef base_type::ii jj;
static jj g(jj v) {return v+1;}
};

FYI, I'm using g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd
0.125)

Sorry if this has already been discused.

Best,
Vicente
 
V

viboes

It does not work because 'base_type' in your template is called "a
dependent name", and when resolving names (like 'base_type' in your
case) in a template the base classes are _not_ searched. Read the FAQ.










It has. Read the FAQ.

V


Thanks a lot for the pointer.

Vicente
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top