modern C++design type 2 typeノート


#include <iostream>
#include <string>
using namespace std;

template<class 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()
{
    string *str = Create("hello", Type2Type<string>());
    cout<<*str<<endl;

    delete str;

    Widget *wid = Create("I am Widget", Type2Type<Widget>());

    delete wid;
}
~                                                                                                                                                     
     Type2Type,          ,          。