在编译时映射两种类型

时间:2011-10-12 12:11:31

标签: c++ templates mapping template-meta-programming

我有一组与一对一关系相关的类型,例如:

TypeA ---> Type1
TypeB ---> Type2 
TypeC ---> Type3

我在编译时就知道了这些关系。

然后,我有一个依赖于这两种类型的模板类:

template<class T1,class T2>
class MyClass
{
  T1 foo;
  T2 bar;
};

现在,我的库的用户将输入类似:

的内容
MyClass<TypeA,Type1> x;

这很不方便,因为两种类型之间存在依赖关系,并且应该足以让用户仅指定第一种类型。

此外,不应该混合这两种类型:

MyClass<TypeA,Type2> y; //it should not compile

我对模板元编程不太熟悉,我觉得这是可行的任务,但我可能错了。

涉及的类型数量很大,但我很乐意在必要时运行脚本来生成代码。

你知道是否有可能或者我在浪费时间吗?你有什么想法让我指出正确的方向吗?

3 个答案:

答案 0 :(得分:6)

template<class T>
struct get_mapped;

template<>
struct get_mapped<TypeA>{
    typedef Type1 type;
};

// and so on....


template<class T>
class MyClass{
    typedef typename get_mapped<T>::type T2;

    T foo;
    T2 bar;
};

答案 1 :(得分:5)

template<class T> struct TypeLetter2TypeDigit;

template<> struct TypeLetter2TypeDigit<TypeA> { typedef Type1 type; };
template<> struct TypeLetter2TypeDigit<TypeB> { typedef Type2 type; };
template<> struct TypeLetter2TypeDigit<TypeC> { typedef Type3 type; };


template<class T1>  // Type2 is not needed
class MyClass 
{ 
  // Type2 is deduced.
  typedef typename TypeLetter2TypeDigit<T1>::type T2;
  T1 foo; 
  T2 bar; 
}; 

答案 2 :(得分:3)

为什么不创建包装类型:

template <typename T1, typename T2>
struct wrapper
{
   typedef T1 type1;
   typedef T2 type2;
};

typedef wrapper<TypeA, Type1> TypeX;
typedef wrapper<TypeB, Type2> TypeY;
typedef wrapper<TypeC, Type3> TypeZ;

然后用户说MyClass<TypeX>;,然后定义:

template <typename T>
class MyClass
{
  typename T::type1 foo;
  typename T::type2 bar;
};

如果您想防止滥用模板,请使用部分专业化:

template <typename> class MyClass; // undefined

template <typename S, typename T>
class MyClass<wrapper<S,T>>
{
  S foo;
  T bar;
};

可以轻松扩展此方法,以将更多编译时数据包含到包装类中。或者,您可以使用std::pair代替成员类型first_typesecond_type

相关问题