Lokiのタイプ認識


これはLokiの中のタイプ識別のテストで、それぞれ普通のタイプ、ポインタのタイプとクラスのメンバーのポインタのタイプをテストします.
以下はテストコード、テスト環境はgcc 4.6である.3
NullType.h
#ifndef _NULLTYPE_INC_
#define _NULLTYPE_INC_

class NullType;

#endif

PointerTraits.h
#ifndef _POINTERTRAITS_INC
#define _POINTERTRAITS_INC 

#include "NullType.h"

template <typename T>
class TypeTraits
{
private:
	template <class U> struct PointerTraits
	{
		enum {result = false};
		typedef NullType PointeeType;
	};

	template <class U> struct PointerTraits<U*>
	{
		enum {result = true};
		typedef U PointeeType;
	};

	template <class U> struct PToMTraits
	{
		enum {result = false};
	};

	template <class U, class V> struct PToMTraits<U V::*>
	{
		enum {result = true};
	};

public:
	enum {isPointer = PointerTraits<T>::result};
	typedef typename PointerTraits<T>::PointeeType PointeeType;

	enum {isMemberPointer = PToMTraits<T>::result};
};


#endif

main.cpp
#include <iostream>
#include <vector>
using namespace std;

#include "PointerTraits.h"

class T
{
public:
	int a;
};

int main(int argc, char *argv[])
{
	bool iterIsPtr = TypeTraits<vector<int>::iterator>::isPointer;
	cout<<"vector<int>::iterator is "<<(iterIsPtr ? "pointer": "type")<<"
"; iterIsPtr = TypeTraits<int*>::isPointer; cout<<"int* is "<<(iterIsPtr ? "pointer": "type")<<"
"; iterIsPtr = TypeTraits<int*>::isMemberPointer; cout<<"int* is member pointer ("<<(iterIsPtr ? "yes": "no")<<")
"; /* * int T::* T int 。 * :int T::* c = &T::a; */ //int T::* c = &T::a; iterIsPtr = TypeTraits<int T::*>::isMemberPointer; cout<<"int* is member pointer ("<<(iterIsPtr ? "yes": "no")<<")
"; return 0; }

コンパイル:g++main.cpp
実行:./a.out
出力:
vector::iterator is type int* is pointer int* is member pointer (no) int T::* is member pointer (yes)