具有默认模板模板参数的模板C ++类

时间:2020-08-28 03:59:21

标签: c++ templates default

我有一个通用类模板MyClass,并希望根据enType值进行专门化的实现。问题是什么也可以使用基于enType值的ServiceCreate的正确语法?

template <enum enType, typename ServiceCreator = CServiceFactory<enum enType>>
class MyClass 
{
public:
    MyClass(const Configuration& cfg) {}
public:
    bool init()  { return false; }
    bool start()  { throwException(); }
    void stop()  { throwException(); }
private:
    void throwException() const { THROW(Exception, "invalid/unsupported mode"); }
};

// I need a specialization impl, so which one of the following 3 possible ways? or all are wrong?
template <typename ServiceCreator>
class MyClass<enType::eCase1> 
{
public:
    MyClass(const CConfiguration& cfg) {
        ServiceCreator(cfg); // it will create something for me by using CServiceFactory<eCase1>
    }
};

template <>
class MyClass<enType::eCase1> 
{
public:
    MyClass(const CConfiguration& cfg) {
        ServiceCreator(cfg); // it will create something for me by using CServiceFactory<eCase1>
    }
};

template <>
class MyClass<enType::eCase1, ServiceCreator<enType::eCase1> 
{
public:
    MyClass(const CConfiguration& cfg) {
        ServiceCreator(cfg); // it will create something for me by using CServiceFactory<eCase1>
    }
};


0 个答案:

没有答案