使用可变参数模板的工厂模式? [英] Factory pattern using variadic template?

查看:155
本文介绍了使用可变参数模板的工厂模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类

template <class T> struct A { /* virtual methods */ };

以及具有各种构造函数的几个具体派生类

and several concrete derived classes with various constructors

// The constructor of B takes 1 input
template <class T>
struct B
    : public A<T>
{
    B() { /* default ctor */ }
    B( T *input ) { /* initializer */ }

    // .. implement virtual methods
}


// The constructor of C takes 2 inputs
template <class T>
struct C
    : public A<T>
{
    double some_member;

    C() { /* default ctor */ }
    C( T *input, double& value ) { /* initializer */ }

    // .. implement virtual methods
}

我创建了一个工厂,该工厂返回指向<$ c的指针$ c> A ,我正在尝试使用可变参数模板将输入转发到所选派生类的构造函数。一切正常,但是我不得不为带有/不带有构造函数输入的情况复制代码,并且我正在寻找一种防止代码重复的方法(请参见下文)。

I created a Factory that returns pointers to A, and I am trying to use variadic templates to forward inputs to the constructor of the selected derived class. It is working fine, but I had to duplicate the code for the cases with/without constructor inputs, and I am looking for a way to prevent code duplication (see below).

template <class T>
struct A_Factory
{
    typedef std::shared_ptr<A> out_type;

    // Version without constructor inputs
    static out_type create( id_type id )
    {
        out_type out;
        switch (id)
        {
            // .. select the derived class
            case Type_B:
                out.reset( new B() );
                break;
        }
        return out;
    }

    // Version with constructor inputs
    template <class... Args>
    static out_type create( id_type id, Args&&... args )
    {
        out_type out;
        switch (id)
        {
            // .. select the derived class
            case Type_B:
                out.reset( new B( std::forward<Args>(args)... ) );
                break;
        }
        return out;
    }
};

很抱歉,这个冗长的问题。

Very sorry for the long question. Any suggestion to make this shorter appreciated.

推荐答案

我们可以使用SFINAE和 std :: is_constructible 类型特征(h / t Y牛)。

We can solve this using SFINAE and the std::is_constructible type trait (h/t Yakk).

我们只需要一个单独的create函数,该函数将分派给其他函数:

We just need one single create function, that will dispatch out to other functions:

template <class... Args>
static std::shared_ptr<A> create( id_type id, Args&&... args )
{
    switch (id) {
    case Type_B:
        return create<B>(std::forward<Args>(args)...);
    case Type_C:
        return create<C>(std::forward<Args>(args)...);
    // ...
}

每个基于标记的 create 函数将调用正确的构造函数,或者如果不存在该函数,则返回nullptr:

Each tag-based create function will either call the correct constructor OR return nullptr if such a one does not exist:

// possible to construct
template <typename T, typename... Args>
std::enable_if_t<
    std::is_constructible<T, Args...>::value,
    std::shared_ptr<T>
>
create(Args&&... args) {
    return std::make_shared<T>(std::forward<Args>(args)...);
}

// impossible to construct
template <typename T, typename... Args>
std::enable_if_t<
    !std::is_constructible<T, Args...>::value,
    std::shared_ptr<T>
>
create(Args&&... ) {
    return nullptr;
}

这篇关于使用可变参数模板的工厂模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆