| 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).
.
|
|
|
|

|
Related Articles |
|
|