I am being driven Nuts by a simple Template specialization problem
Here is the code, Its intent is straightforward,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
using namespace std;
template<int const P,int const Q>
struct cWrapper {
#if 0
#define P 5
#define Q 8
#endif
template<int A,int B>
struct cInner {
static void X(int SpecifiedA,int SpecifiedB){
cout << " Generic (" << SpecifiedA << " " << SpecifiedB << ")"
<< " A == " << A
<< " B == " << B
<< "\n" ;
}
};
template<>
struct cInner<P,Q> {
static void X(int SpecifiedA,int SpecifiedB){
cout << " Special (" << SpecifiedA << " " << SpecifiedB << ")"
<< " A == " << "P"
<< " B == " << "Q"
<< "\n" ;
}
};
template<int B>
struct cInner<P,B> {
static void X(int SpecifiedA,int SpecifiedB){
cout << " Special (" << SpecifiedA << " " << SpecifiedB << ")"
<< " A == " << "P"
<< " B == " << B
<< "\n" ;
}
};
template<int A>
struct cInner<A,Q> {
static void X(int SpecifiedA,int SpecifiedB){
cout << " Special (" << SpecifiedA << " " << SpecifiedB << ")"
<< " A == " << A
<< " B == " << "Q"
<< "\n" ;
}
};
};
int main(){
cWrapper<5,8>::cInner<9,9>::X(9,9);
cWrapper<5,8>::cInner<5,9>::X(5,9);
cWrapper<5,8>::cInner<9,8>::X(9,8);
cWrapper<5,8>::cInner<5,8>::X(5,8);
return 0;
}
|
Here is the output without the macro definition overriding the template
parameters (the Wrong behaviour) :-
Special (9 9) A == P B == 9
Generic (5 9) A == 5 B == 9
Generic (9 8) A == 9 B == 8
Special (5 8) A == P B == Q
Here is the output with the macro definitions overriding the template
parameters (the Right behaviour)
Generic (9 9) A == 9 B == 9
Special (5 9) A == P B == 9
Special (9 8) A == 9 B == Q
Special (5 8) A == P B == Q
I am using the MS VC++ 2008 Express, on XP all up todate, and have gotten the same behaviour from 2010 Express.
Thanks In advance, this has got me stumped, or am I missing summit.
Nick