473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inherits mixin. What is wrong?

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
Jul 14 '08 #1
2 1601
viboes wrote:
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?
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.
>
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.
It has. Read the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 14 '08 #2
On 14 juil, 22:38, Victor Bazarov <v.Abaza...@comAcast.netwrote:
viboes wrote:
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?

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.


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.

It has. Read the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thanks a lot for the pointer.

Vicente
Jul 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1609
by: zimba | last post by:
Hello ! If somebody is interested, here is a small hack I've done today. There are still some curious effects, but I'm pretty satisfied by the results, since PHP is not very flexible. Let...
5
2719
by: Udo Gleich | last post by:
Hi, I try to implement mixin classes. Thats why I need to make a new class at runtime. --tmp.py------------------------------------- import new class K1(object):
1
2566
by: Mac | last post by:
I have a MixIn class which defines a method foo(), and is then mixed in with another class by being prepended to that class's __bases__ member, thus overriding that class's definition of foo(). In...
0
1320
by: Paolino | last post by:
I had always been negative on the boldeness of python on insisting that unbound methods should have been applied only to its im_class instances. Anyway this time I mixed in rightly, so I post this...
0
345
by: barnesc | last post by:
>So mixins are just a sub-class of sub-classing? > >I've just found this: > > >A mixin class is a parent class that is inherited from - but not as >a means of specialization. Typically, the...
6
2465
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
3
1453
by: Ed Leafe | last post by:
In Dabo, we create cursor classes that combine the backend-specific dbapi cursor class with our own mixin class that adds framework- specific behaviors. This has been working well for a couple of...
2
2186
by: ish | last post by:
I think this is more of a style question than anything else... I'm doing a C++ wrapper around a C event library I have and one of the items is a timer class, I'm also using this task to learn C++....
1
1415
by: Ole Nielsby | last post by:
Given these 3 classes class A {virtual void a(){}}; class B {virtual void b(){}}; class C: public A, public B {}; I want the offset of B in C, as a size_t value, and preferably as a constant...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.