weird enum initialization in template class

A

Adam S

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;
}
 
R

Ron Natalie

Adam said:
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).
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top