Factory pattern using variadic template?

倖福魔咒の 提交于 2019-11-28 22:00:20
Barry

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!