Compile time if-else statement

Following Modern C++.


using namespace std;

struct int2type
enum { value = v };

class Factory
T* create(int2type)
cout << "new T" << endl; return new T; } T* create(int2type)
cout << "new T(\"hi\")" << endl; return new T("hi"); } public: T* create() { return create(int2type()); }

int main()
Factory f;
Factory f2;

string* s = f.create();
cout << *s << endl; delete s; s = f2.create(); cout << *s << endl; delete s; return EXIT_SUCCESS; } [/code] [output] C:\> a.exe
new T

new T(“hi”)
Hit any key to close this window…

int2type class was used to convert the given parameter into type parameter. According to the type of int2type, proper method for creating T is called.

Similar Posts:

Post a Comment

Your email is never published nor shared.