是否有可变参数模板的替代品

时间:2013-10-02 10:17:18

标签: c++ variadic-templates

我有一些像这样的代码:

// Factory.h

typedef std::uint8_t FactoryId;
const FactoryId FACTORY_ID_MAX_VALUE = UINT8_MAX;

template <typename TBaseProduct>
class IFactory {
public:
    virtual TBaseProduct* create() = 0;
};

template <typename TProduct, typename TBaseProduct>
class Factory : public IFactory<TBaseProduct> {
public:
    virtual TBaseProduct* create() override {
        return new TProduct;
    }
};

template <typename TProduct, typename TBaseProduct>
class FactoryProduct : public TBaseProduct {
public:
    static Factory<TProduct, TBaseProduct> factory;
};

template <typename TFactoryTable, typename TBaseProduct>
class FactoryTable {
public:
    typedef IFactory<TBaseProduct> BaseFactory;
    typedef BaseFactory* BaseFactoryPtr;

    FactoryTable(): factorys(nullptr) {}

    ~FactoryTable() { delete[] factorys; }

    BaseFactory& get(FactoryId factoryId) {
        if (factoryId > maxFactoryId) {
            throw std::exception("out of range");
        }
        return *factorys[factoryId];
    }

protected:
    template <std::size_t factoryCount>
    void init(BaseFactoryPtr (&factorys)[factoryCount]) {
        init(factorys, factoryCount);
    }

    void init(BaseFactoryPtr* factorys, std::size_t factoryCount) {
        assert(factorys != nullptr);
        assert(factoryCount > 0 && factoryCount - 1 <= FACTORY_ID_MAX_VALUE);

        this->factorys = new BaseFactoryPtr[factoryCount];
        std::memcpy(this->factorys, factorys, sizeof(BaseFactoryPtr) * factoryCount);
        this->maxFactoryId = factoryCount - 1;
    }

private:
    BaseFactoryPtr* factorys;
    FactoryId maxFactoryId;
};

// Foo.h

class BaseFoo {};

class Foo1 : public FactoryProduct<Foo1, BaseFoo> {};
class Foo2 : public FactoryProduct<Foo2, BaseFoo> {};

class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo> {
public:
    FooFactoryTable() {
        IFactory<BaseFoo>* table[] = {
            &Foo1::factory,
            &Foo2::factory,
        };
        init(table);
    }
};

为了向init FactoryTable&lt;&gt;提供工厂数组,我必须在FooFactoryTable中手动创建一个数组(IFactory * table [])。所以我使用variadic模板来避免这种情况:

template <typename TFactoryTable, typename TBaseProduct, typename... MTProduct>
class FactoryTable {

    // the visible of the two init() is changed from protected to private.
    // except this, the only changed member is the constructor

    FactoryTable(): factorys(nullptr) {
        IFactory<BaseFoo>* table[] = {
            (&MTProduct::factory)...
        };
        init(table);
    }
};

这样我就可以像这样简单地实现“FooFactoryTable”,并隐藏FooFactoryTable关于“FactoryProduct&lt;&gt; :: factory”的知识

class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo, 
    Foo1,
    Foo2> {};

我的问题是,是否有另一种方法可以在不使用可变参数模板的情况下实现“FactoryTable”?因为Visual Studio 2012(v110)不支持可变参数模板,“'Microsoft Visual C ++编译器2012年11月CTP'仅用于测试目的。”我担心其他编译器也可能遇到与visual studio 2012(v110)相同的问题。 “其他编译器”是那些针对android或iphone的编译器。

请注意:重新实现“FactoryTable”的主要原因是使FooFactoryTable尽可能简单。

2 个答案:

答案 0 :(得分:2)

使用VS2013 RC。它支持可变参数模板。与所有针对当前其他移动平台的编译器一样。

不要使用不受支持的功能,你最终会得到一些不完全符合你想要的讨厌的黑客攻击。

答案 1 :(得分:0)

Visual Studio确实支持&#34; faux&#34; variadic模板,允许最多5-10(默认为8)参数。您可以在VS2012中使用variadics,除非您确实需要使用大量类型参数来实例化模板。