M
mars
Alexandrescu(2000) advance a method about mapping intergral constants
to types like this:
template<int v>
struct Int2Type {
enum { value = v };
};
Why cann't use like this:
template<int v>
struct Int2Type {
};
then we can use it like following:
////////////////////////////////////////////////
template<typename T>
class MyClass {
public:
void dothings() { fun(T()); }
private:
void fun(const Type<true> &a) { cout << "case 1: compiling!\n"; }
void fun(const Type<false> &a) { cout << "case 2: compiling!\n";
Gerald }
void fun(const Type<2> &a) { cout << "case3: compiling!\n"; }
};
int main() {
MyClass<Int2Type<true> > obj1;
obj1.dothings();
// MyClass<Int2Type<false> > obj2;
// obj2.dothings();
// MyClass<Int2Type<2> > obj3;
// obj3.dothings();
return 0;
}
It also works.
another question is that:
Can I use 'class' to replace 'struct' in the definition?
Thank you in advance.
to types like this:
template<int v>
struct Int2Type {
enum { value = v };
};
Why cann't use like this:
template<int v>
struct Int2Type {
};
then we can use it like following:
////////////////////////////////////////////////
template<typename T>
class MyClass {
public:
void dothings() { fun(T()); }
private:
void fun(const Type<true> &a) { cout << "case 1: compiling!\n"; }
void fun(const Type<false> &a) { cout << "case 2: compiling!\n";
Gerald }
void fun(const Type<2> &a) { cout << "case3: compiling!\n"; }
};
int main() {
MyClass<Int2Type<true> > obj1;
obj1.dothings();
// MyClass<Int2Type<false> > obj2;
// obj2.dothings();
// MyClass<Int2Type<2> > obj3;
// obj3.dothings();
return 0;
}
It also works.
another question is that:
Can I use 'class' to replace 'struct' in the definition?
Thank you in advance.