473,890 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template inheritance and enum values

Mmmh... I was trying some code, and I bumped in a strange behaviour.
The following piece of code compiles normally :
//////// CODE STARTS /////////////////

#include <iostream>

class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

class D : public B {
public :
void f( int = ONE );
};

void
D::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D d;
d.f();
}

//////// CODE ENDS ///////////////////

Then, if I "templatize " the code, nothing works anymore:

//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?

With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.

Anyway, I think that the ``correct'' behaviour is the one showed with
the non-templatized classes, so how can I change the code of the
second example in order to make it work?

Thanks for any help,
Matteo

PS : I'm using g++ 3.4.3
Jul 22 '05 #1
3 1784
Matteo Settenvini wrote in news:8eb2e74a.0 412161457.118bb 672
@posting.google .com in comp.lang.c++:
Then, if I "templatize " the code, nothing works anymore:

This is in the FAQ (its a recient change I beleive).
//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
void f( int = B<T>::ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
std::cout << a << ' ' << B<T>::TWO << ' ' << B<T>::THREE << std::endl;
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?


Names that are dependant on template paramiters can't be looked up
when the template is first parsed, so the extra qualification
is needed.

Also avoid, if at all possible, using all UPPERCASE identifiers
for anything but macro's (#define's), its a rule (convention) that
most c++ programmers follow and it will at some point save you from
some very difficult to find and understand bugs.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Matteo Settenvini wrote:
Mmmh... I was trying some code, and I bumped in a strange behaviour.
The following piece of code compiles normally :
//////// CODE STARTS /////////////////

#include <iostream>

class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

class D : public B {
public :
void f( int = ONE );
};

void
D::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D d;
d.f();
}

//////// CODE ENDS ///////////////////

Then, if I "templatize " the code, nothing works anymore:

//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
'ONE' is not found during name lookup because it's a dependent
name and for templates base class scopes are not searched. You
need to qualify that name:

void f(int = B<T>::ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
Same here. 'TWO' and 'THREE' won't be found unless you qualify
them.
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?
Base class scope is not searched if it's a dependent type (and in your
case 'Base<T>' depends on 'T'.
With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.
I don't understand the last sentence. If you don't use what?
Anyway, I think that the ``correct'' behaviour is the one showed with
the non-templatized classes, so how can I change the code of the
second example in order to make it work?


See above.

V
Jul 22 '05 #3


With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.

I don't understand the last sentence. If you don't use what?


Templates. Sorry, I ain't a native english speaker and sometimes I mess
up the sentences.

Thanks of your answers!
Jul 22 '05 #4

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

Similar topics

0
1160
by: Gianni Mariani | last post by:
I'm trying to make a generic factory that provides a placement new as well as the accompanying destructor. To do a down-cast I used a static_cast but it turns out that this is not always a possible because of virtual inheritance. What I'd like to do is provide an automatic way of deciding to use a dynamic or static cast depending on if it's possible to static_cast. The jist of what I want to do is below but I know it's wrong. Any...
0
1110
by: gao_bolin | last post by:
I have a library that has a class Signal<T> whose data type is templated. The class comes with a whole bunch of functions and objects to do various processing on the signal. What I would like to do is a DSignal class that does essentially the same thing, but whose type is unknown at compile type. Since I have already this template library with everything I want in it, I would like to maximally reuse it and minimize overheads. It's the...
0
1461
by: Michael Andersson | last post by:
Given a set of classes class A { enum [ ID = 0x0001} }; class B { enum [ ID = 0x0002} }; class B { enum [ ID = 0x0004} }; I wish to generate a composite class, perhaps using something like Alexandrescu's typelists (pseudo-code:)
11
2497
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: class A {}; template <typename T> struct Is_C { static const bool value = false;
1
2226
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
1
2365
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one single class. Instead I have to separate implementation for binary and ascii in two different classes as I don't know how to use the traits technique for constructors. Any pointers very welcome !
6
2944
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template index_properties<unsigned int>, i get a compiler-error, complaining about the template-recursion of __index_properties__. when i add a partially specialized template (the three commented lines) to stop the template-recursion, it works. does anyone how to...
8
1554
by: vectorizor | last post by:
Hi all, I have a slight problem with the usage of function templates. It shoud be really easy for you guys to explain me what's wrong. I have one base class and 2 derived classes: /*code*/ template <typename Tstruct Image {...}; template <typename Tstruct GrayImage : public Image<T{...};
7
7991
by: Kevin | last post by:
I'm creating a template to support state machines. In doing so, I need to pass an enumeration for the number of transitions and a non type parameter for the range of the enum (to allow me to allocate an array of pointers big enough to hold one entry for each transition). My template declaration looks roughly like this: template <class TRANSITION, int NUMTRANSITIONS> class StateMachine { ***SNIP*** void RegisterTransition(TRANSITION...
0
9812
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10799
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8004
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5832
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4655
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3263
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.