相关疑难解决方法(0)

将std :: array作为模板可变参数函数的参数传递

我试图了解C++ 11中的可变参数模板.我有一个类,它基本上是一个包装器std::array.我希望能够将函数对象(理想情况下为lambdas)传递给成员函数,然后将std::array传递的元素作为函数对象的参数.

我用a static_assert来检查参数的数量是否与数组的长度相匹配,但我想不出将元素作为参数传递的方法.

这是代码

#include <iostream>
#include <array>
#include <memory>
#include <initializer_list>

using namespace std;

template<int N, typename T>
struct Container {
    template<typename... Ts>
    Container(Ts&&... vs) : data{{std::forward<Ts>(vs)...}} {
        static_assert(sizeof...(Ts)==N,"Not enough args supplied!");
    }

    template< typename... Ts>
    void doOperation( std::function<void(Ts...)>&& func )
    {
        static_assert(sizeof...(Ts)==N,"Size of variadic template args does not match array length");

        // how can one call func with the entries
        // of data as the parameters (in a way generic with N) …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

9
推荐指数
1
解决办法
3380
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1