Type2Type

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
template<typename T>
struct Type2Type
{
  typedef T OrgT;
};
class Widget
{
public:
  Widget(string str,int n){
    cout<<str<<":"<<n<<endl;
  }
};
template<class T,class U>
T* Create(const U& org,Type2Type<T>)
{
  return new T(org);
}
template<class T>
Widget* Create(const T& org,Type2Type<Widget>)
{
  return new Widget(org,-1);
}
int main(int argc, char *argv[])
{
    string* str = Create("Hello world",Type2Type<string>());
    cout<<*str<<endl;
    delete str;
    Widget* wid = Create("I am Widget",Type2Type<Widget>());
    delete wid;
    system("PAUSE");
    return EXIT_SUCCESS;
}

你可能感兴趣的:(Type2Type)