简体   繁体   English

部分专业化模板作为特征成员类型

[英]partially specialized template as a trait member type

I want to choose a template from a trait class, like in the following: 我想从特征类中选择一个模板,如下所示:

template<typename T>
class JobTypeA { };

template<typename T>
class JobTypeB { };

template<typename T>
class JobTraits
{
    /* nothing */
};

class A { };

template<>
class JobTraits<A>
{
    typedef JobTypeA Type;
};

class B {};

template<>
class JobTraits<B>
{
    typedef JobTypeB Type;
};

class JobTarget1 { };
class JobTarget2 { };

template<typename T, typename U>
class JobUser
{
public:
    typedef typename JobTraits<T>::Type<U> JobType;

    void doSomething (void)
    {
        JobType j;
        /*... */
    }
};

int
main (void)
{
    JobUser<B, JobTarget1> j;
}

The above wont compile because of the "typedef of an incomplete type" in the specialized traits classes. 由于特殊特征类中的“不完整类型的typedef”,因此不会编译以上内容。 I got this working using 'alias templates' in std=c++11 with g++ 4.7.2. 我在带有g ++ 4.7.2的std = c ++ 11中使用“别名模板”来工作。 However VS2010 does not support it yet. 但是VS2010还不支持它。 Are there any workarounds to achieve the same without 'alias templates'. 没有“别名模板”,是否有任何变通办法可以实现相同的目的。

How about 怎么样

template<typename U>
struct Type
{
   typedef JobTypeA<U> type;
};

and

template<typename U>
struct Type
{
   typedef JobTypeB<U> type;
};

Usage: 用法:

typedef typename JobTraits<T>::template Type<U>::type JobType;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM