Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an abstract class

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
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
406 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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)...);
    // ...
}

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...