可变参数类参数成员变量的异构存储 [英] Heterogenous storage of variadic class parameter member variables

查看:31
本文介绍了可变参数类参数成员变量的异构存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可变参数类模板,用于为可变数量的类创建顶级类.要进入顶级类的每个类都是从基类派生的,因为它们具有通用功能.我不知道在父类中存储派生类的最佳方式,但仍然能够访问派生类的全部功能.

I have a variadic class template that is used to create a top-level class for a variable number of classes. Each class that is to go in the top-level class is derived from a base class, as there is common functionality for them. I don't know the best way to store the derived classes in the parent class, but still be able to access the full functionality of the derived class.

如果我将可变参数 args 存储在向量中,它们将全部存储为基类,并且我无法访问派生功能.如果我将它们存储在一个元组中,我将无法弄清楚如何通过派生类型访问这些函数.如果我尝试访问它们如此处讨论的那样,则 make_unique 不是可用(C++14?).

If I store the variadic args in a vector, they'll all be stored as a base class and I can't access the derived functionality. If I store them in a tuple, I can't work out how to access the functions by derived type. If I try to access them as discussed here on SO then make_unique isn't available (C++14?).

所以,我想做以下事情:

So, I want to do the following:

class BaseElement {
public:
    virtual int polymorphicFunction() {return 0;};
};

class DerivedElement1 : public BaseElement {
public:
    virtual int polymorphicFunction() {return 1;};
}

class DerivedElement2 : public BaseElement {
public:
    virtual int polymorphicFunction() {return 2;};
}

template<typename... systems>   // systems will always be of derived class of BaseElement
class System {
    System() : subsystems(systems{}...) {} ;  // all variadic elements stored in tuple
    // tuple used below, the system elements don't need to be stored in a container, I just want to access them 
    // I'd be happy to use a vector or access them directly as a member variable
    // provided that I can access the derived class.  I can't use RTTI.
    const std::tuple<systems...> subSystems;

    // pointer or reference, I don't mind, but pd1/2 will always exist, 
    // (but perhaps be NULL), even if there is no derived element passed to the template parameter
    DerivedElement1 *pd1; 
    DerivedElement2 *pd2;
};

//Desired usage
System<DerivedElement1> sys;  // sys->pd1 == &derivedElement1WithinTuple, sys->pd2 == NULL
System<DerivedElement2> sys;  // sys->pd2 == &derivedElement2WithinTuple, sys->pd2 == NULL
System<DerivedElement1, DerivedElement2> sys;  // sys->pd1 == &derivedElement1WithinTuple, sys->pd1 == &derivedElement1WithinTuple

有人对我如何实现这一目标有任何建议吗?

Does anyone have any suggestions as to how I might achieve this please?

推荐答案

With:

#include <cstdint>
#include <type_traits>
#include <tuple>

namespace detail
{

template <typename T, typename... Ts> struct get_index;

template <typename T, typename... Ts>
struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};

template <typename T, typename Tail, typename... Ts>
struct get_index<T, Tail, Ts...> :
    std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {};

template <typename T>
struct get_index<T> : std::integral_constant<std::size_t, 0> {}; // Not found


template <std::size_t N, typename... Ts>
constexpr
auto
safe_get(const std::tuple<Ts...>& t) noexcept
-> typename std::enable_if<N < sizeof...(Ts), decltype(&std::get<N < sizeof...(Ts) ? N : 0>(t))>::type
{
    return &std::get<N>(t);
}

template <std::size_t N, typename... Ts>
constexpr
auto
safe_get(const std::tuple<Ts...>&) noexcept
-> typename std::enable_if<sizeof...(Ts) <= N, nullptr_t>::type
{
    return nullptr;
}

}

你可能有:

template <typename... systems>
class System {
public:
    constexpr System() :
        subSystems(),
        pd1(detail::safe_get<detail::get_index<DerivedElement1, systems...>::value>(subSystems)),
        pd2(detail::safe_get<detail::get_index<DerivedElement2, systems...>::value>(subSystems))
    {}

    const std::tuple<systems...> subSystems;
    const DerivedElement1 *pd1;
    const DerivedElement2 *pd2;
};

这篇关于可变参数类参数成员变量的异构存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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