weird enum initialization in template class



 DEVELOP > c-Plus-Plus > weird enum initialization in template class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Adam S"
Date: 31 Jan 2008 03:29:09 AM
Object: weird enum initialization in template class
Is it ok to use the bit shift operator when initializing enum values ?
The code below does some unexpected initialization of N in template
class ClassTwo. I pretty sure its a bug in Borland C v5.6 as GNU C++
v3.4.4 always prints the first line "1024";
----------------------------------------------------
#include <iostream>
template<int N> class ClassOne {
public:
void printN() { // print value of N
std::cout << N <<"\n";
}
};
template<int> class ClassTwo {
enum {N = 1<<10 };
enum {U = 1024 };
public:
ClassOne<N> testone1;
ClassOne<U> testone2;
ClassOne<1024> testone3;
ClassOne<(1<<10)> testone4;
void printN() {
std::cout << N <<"\n";
}
};
main() {
ClassTwo<0> tp;
tp.testone1.printN(); // prints "0"
tp.testone2.printN(); // prints "1024"
tp.testone3.printN(); // prints "1024"
tp.testone4.printN(); // prints "1024"
tp.printN(); // prints "1024"
return 0;
}
.

User: "Ron Natalie"

Title: Re: weird enum initialization in template class 31 Jan 2008 05:49:59 AM
Adam S wrote:

Is it ok to use the bit shift operator when initializing enum values ?

The code below does some unexpected initialization of N in template
class ClassTwo. I pretty sure its a bug in Borland C v5.6 as GNU C++
v3.4.4 always prints the first line "1024";


Looks fine other than the absence of a return type of int on main.
I would theorize it isn't a problem with the enum, but rather some
confusion on Borland's template generator over the N token. Try
changing N to Q either in classone or classtwo (but not both).
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER