【问题标题】:arduino parameter pack working without tuplearduino 参数包在没有元组的情况下工作
【发布时间】:2020-02-02 08:52:54
【问题描述】:

我正在尝试做一个可以返回参数包的草图。我在这里找到了一个参考: tuple to parameter pack

我将其修改为更加通用,并且可以将任何类型的对象返回到 void 函数指针。

也就是说,现在我正在使用 arduino DUE 进行测试,并且该板支持元组。但是 arduino uno 没有。

所以,根据这篇文章: variadic data structure 我决定使用 UNO 支持的我自己的微元组数据结构。该结构自行工作。

到目前为止的代码:

#include <tuple>

template<size_t idx, typename T>
struct microTupleGetHelper;

template<typename ... T>
struct microTuple
{
};

template<int ...> struct seq {};

template<int N, int ...S> struct gens : gens<N - 1, N - 1, S...> { };

template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };



template<typename T, typename ... Rest>
struct microTuple<T, Rest ...>
{
microTuple(const T& first, const Rest& ... rest)
    : first(first)
    , rest(rest...)
{}

T first;
microTuple<Rest ... > rest;

template<size_t idx>
auto get() ->  decltype(microTupleGetHelper<idx, microTuple<T,Rest...>>::get(*this))
{
    return microTupleGetHelper<idx, microTuple<T,Rest...>>::get(*this);
}
};

template<typename T, typename ... Rest>
struct microTupleGetHelper<0, microTuple<T, Rest ... >>
{
    static T get(microTuple<T, Rest...>& data)
    {
        return data.first;
    }
};

template<size_t idx, typename T, typename ... Rest>
struct microTupleGetHelper<idx, microTuple<T, Rest ... >>
{
    static auto get(microTuple<T, Rest...>& data) ->  decltype(microTupleGetHelper<idx-1, 
microTuple<Rest ...>>::get(data.rest))
    {
        return microTupleGetHelper<idx-1, microTuple<Rest ...>>::get(data.rest);
    }
};



template <typename ...Args>
struct paramsPack
{
//std::tuple<Args...> params;
microTuple<Args...> params;

void (*func)(Args...);

/*    template<int ...S>
auto callFunc(seq<S...>) -> decltype(this->func(std::get<S>(this->params) ...))
{
    return func(std::get<S>(params) ...);
}*/

template<int ...S>
auto callFunc(seq<S...>) -> decltype(this->func(this->params.get<S>() ...))
{
    return func(params.get<S>() ...)
}

auto delayed_dispatch() -> decltype(this->callFunc(typename gens<sizeof...(Args)>::type()))
{
    return this->callFunc(typename gens<sizeof...(Args)>::type()); // Item #1
}

};

错误来自自动调用函数(..),它是:

microTuple:72:73: error: expected primary-expression before ')' token

 auto callFunc(seq<S...>) -> decltype(this->func(this->params.get<S>() ...))

                                                                     ^

【问题讨论】:

    标签: c++ c++11 arduino variadic parameter-pack


    【解决方案1】:

    params 的类型依赖于模板参数,所以编译器在解析.get 时无法知道它是否是模板。你需要明确告诉编译器它是一个模板:

    auto callFunc(seq<S...>) -> decltype(this->func(this->params.template get<S>() ...))
    

    【讨论】:

    • 不错。我以前会看到这种语法。更多的空间和 .template 功能。我必须将它添加到return语句中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多