c++ 按索引访问可变参数模板中的类型

qvtsj1bj  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(125)

我想通过索引获取可变参数模板中的类型。索引被指定为模板参数。我设法找到了一个有效的“hack”,但我相信它不符合可变参数模板编程的精神。此外,它使用额外的内存。
下面是代码和一些解释:

template <typename... InputPortTypes>
class PipelineReceiver
{

protected:

    // This tuple is used for storing types only
    // Hence, I would like to get rid of it, but I am not sure how.
    std::tuple<
    std::function<std::unique_ptr<InputPortTypes> (int)>...
    > InputPortsTuple;

    // This vector is used for storing the actual objects
    // This is needed to be able to access/change its elements
    // during run time later on.
    // The vector is used for storage of function pointers (i.e. of type std::function)
    // that represent methods of another object upstream the pipeline.
    std::vector<boost::any> InputPortsVector;

public:

    PipelineReceiver()
        {
            // create an empty vector of the required size
            InputPortsVector.resize(sizeof...(InputPortTypes));
        }

    void connectPorts(int InputPortIndex, boost::any c_OutputPort)
        {
            // connect ports
            InputPortsVector[InputPortIndex] = c_OutputPort;
        }

     // this function needs to be modified to avoid using InputPortsTuple
    template<int N>
    void getInputPortValue(void)
        {
            std::cout <<
                *boost::any_cast<decltype(std::get<N>(this -> InputPortsTuple))>(
                    InputPortsVector[N]
                    )(0) <<
                std::endl;
        }

};

我想删除对象InputPortsTuple,并将其替换为某种形式的递归过程,用于推断getInputPortValue中的类型。
理想情况下,我希望N是一个动态参数而不是模板参数,但是我不确定这是否可行。

ymdaylpp

ymdaylpp1#

您可以简单地滥用std::tuple_element

typename std::tuple_element<N, std::tuple<InputPortTypes...>>::type

注意:如果你会使用C++14,

std::tuple_element_t<N, std::tuple<InputPortTypes...>>

是做同样事情的更好的方法。不过,还不是所有的通用编译器都知道这一点。

9gm1akwq

9gm1akwq2#

如果您可以制作自己的模板,则可以创建自己的std::tuple_element变体,它直接接受类型列表,而不必将其 Package 在std::tuple中。

#include <type_traits>
using std::size_t;

template<std::size_t N, class ...T>
struct typelist_element;

// recursive case
template<std::size_t N, class Head, class... Tail>
struct typelist_element<N, Head, Tail...> : typelist_element<N-1, Tail...>
{ 
    static_assert(N < (sizeof...(Tail) + 1), "N out of bounds");
};

// base case
template<class Head, class... Tail >
struct typelist_element<0, Head, Tail...> 
{
    using type = Head;
};

// error out of bounds, only here to silence compiler warnings about undefined template
template<>
struct typelist_element<0> 
{
    using type = void;
};

template<std::size_t N, class ...T>
using typelist_element_t = typename typelist_element<N, T...>::type;

然后就

boost::any_cast<typelist_element_t<N, InputPortTypes...>>(InputPortsVector[N])(0)

注意效率,静态Assert将为每个递归求值,所以在那里使用它不是最好的。

相关问题