テンプレートパラメータtagの使い方


//     /            tag,
//    tag     。
//made by davidsu33
//             ,      
//   ,          ,     
//      ,   tag   


//                


#include "stdafx.h"
#include <iostream>
#include <cassert>


using namespace std;


template<class Tag> class Instance;


template<class Tag>
class Instance
{
public:
	~Instance(){};


	static Instance &getInstance()
	{
		static Instance  s_instance;
		return s_instance;
	}


	void doOne()
	{
		cout<<"do One"<<endl;
	}


	void doTwo()
	{
		cout<<"do Two"<<endl;
	}


private:
	Instance(){};
	Instance& operator=(const Instance&);
	Instance(const Instance&);
};


struct TagOne{};
struct TagTwo{};
struct TagThree{};


typedef Instance<TagOne> InstanceOne;
typedef Instance<TagTwo> InstanceTwo;


template<>
class Instance<TagThree>
{
public:
	void doOne()
	{
		cout<<"do Three"<<endl;
	}


	void doTwo()
	{
		cout<<"do Three"<<endl;
	}
};


void test_instance()
{
	InstanceOne &one = InstanceOne::getInstance();
	InstanceTwo &two = InstanceTwo::getInstance();


	void* ptrOne = static_cast<void*>(&one);
	void *ptrTwo = static_cast<void*>(&two);


	assert(ptrOne != ptrTwo);


	Instance<TagThree> three1, three2, three;
	three.doOne();
	three.doTwo();
}




int _tmain(int argc, _TCHAR* argv[])
{
	test_instance();


	getchar();
	return 0;
}