Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从可变模板调用函子_C++_C++11_Variadic Templates - Fatal编程技术网

C++ 从可变模板调用函子

C++ 从可变模板调用函子,c++,c++11,variadic-templates,C++,C++11,Variadic Templates,是否可以编写可变模板类 template<typename Functor, int... D> struct Foo { void bar() { // ??? } }; 模板 结构Foo { 空条() { // ??? } }; 这相当于 template<typename Functor, int D0> struct Foo<Functor, D0> { void bar() {

是否可以编写可变模板类

template<typename Functor, int... D>
struct Foo
{
    void bar()
    {
        // ???
    }
};
模板
结构Foo
{
空条()
{
// ???
}
};
这相当于

template<typename Functor, int D0>
struct Foo<Functor, D0>
{
    void bar()
    {
        Functor f;
        double d0[D0];
        f(d0);
    }
};

template<typename Functor, int D0, int D1>
struct Foo<Functor, D0, D1>
{
    void bar()
    {
        Functor f;
        double d0[D0];
        double d1[D1];
        f(d0, d1);
    }
};

// And so on...
模板
结构Foo
{
空条()
{
函子f;
双d0[d0];
f(d0);
}
};
模板
结构Foo
{
空条()
{
函子f;
双d0[d0];
双d1[d1];
f(d0,d1);
}
};
//等等。。。
也就是说,传递给函子的参数数等于模板参数数。参数应该在堆栈上分配。

这就是您想要的吗

template<typename Functor, int... D>
struct Foo
{
    template<std::size_t N>
    std::array<double, N> create()
    {
       return std::array<double, N>();
    }

    void bar()
    {
       Functor f;
       f(create<D>()...);
    }
};
模板
结构Foo
{
模板
std::array create()
{
返回std::array();
}
空条()
{
函子f;
f(create()…);
}
};

通过
std::tuple跟踪堆栈上带有参数的版本

// Helper class to be able to use expansion of std::get<Index>(tuple)
template <int... Is> struct index_sequence {};

// Following create index_sequence<0, 1, 2, .., sizeof...(Is) - 1>
template <int Index, int... Is>
struct make_index_sequence { // recursively build a sequence of indices
    typedef typename make_index_sequence<Index - 1, Index -1, Is...>::type type;
};

template <int... Is>
struct make_index_sequence<0, Is...> { // stop the recursion when 0 is reached
    typedef index_sequence<Is...> type;
};

template<typename Functor, int... Ds>
struct Foo
{
    void bar()
    {
        bar(typename make_index_sequence<sizeof...(Ds)>::type());
    }
private:
    template <int... Is>
    void bar(index_sequence<Is...>)
    {
        Functor f;
        std::tuple<double[Ds]...> t; // std::tuple<doudle[D0], double[D1], ..>
        f(std::get<Is>(t)...);       // f(std::get<0>(t), std::get<1>(t), ..);
    }
};
//帮助器类能够使用std::get(元组)的扩展
模板结构索引_序列{};
//下面是创建索引的顺序
模板
struct make_index_sequence{//递归地构建索引序列
typedef typename make_index_sequence::type type;
};
模板
结构make_index_sequence{//达到0时停止递归
类型定义索引_序列类型;
};
模板
结构Foo
{
空条()
{
条(typename make_index_sequence::type());
}
私人:
模板
空栏(索引顺序)
{
函子f;
std::tuple t;//std::tuple
f(std::get(t);//f(std::get(t),std::get(t),…);
}
};
也许这样可以:

template <typename Functor, int i, int... others>
struct Foo
{
  template <typename ...T>
  void bar(T ... rest)
  {
    double d[i];
    Foo<Functor, others...> foo;
    foo.bar(rest..., d);
  }
};

template <typename Functor, int i>
struct Foo<Functor, i>
{
  template <typename ...T>
  void bar(T ... rest)
  {
    double d[i];
    Functor f;
    f(d, rest...);
  }
};
模板
结构Foo
{
模板
空杆(T…休息)
{
双d[i];
富富,;
富吧(其余,d);;
}
};
模板
结构Foo
{
模板
空杆(T…休息)
{
双d[i];
函子f;
f(d,休息…);
}
};

@Jarod42是的,也许吧。但是你能评论一下为什么不行吗?我误读了你的解决方案。。。请注意,您的
bar
签名与OP不同。您可以使用无关的参数调用它。@Jarod42它是不同的,但如果这是一个问题,您可以将其重命名为其他名称,并从bar()调用它。我猜这是一个干净简单的解决方案IMHO。但是OP要求的是一个稍微不同的东西,除了解包到函子之外,他还需要每个数组的本地副本。这是一个非常好的实现。您使用的技术是在
get
中删除类型并使用类型数作为
索引,对吗?你能解释一下吗?我很难理解
索引
扩展。如果你能发布一个带有解释的编辑,那就太好了。非常感谢:-)索引扩展是一种迭代
std::tuple
的技术。你可以看看。哦,我的天,因为有点误读,我花了很多时间来理解你的代码(零专门化代码)。误读的是
struct index{typedef index type;}
部分,我没有意识到您没有调用
::type
on
index
,这使我重复使用
index
(导致错误)。我在踢自己:-)有趣的技术。谢谢