元编程模板递归不递归(编辑重要)
我尝试使用元编程递归调用函数 void set (...)。
问题是它似乎只调用一次。
template <int N>
struct GEN
{
enum {value = GEN<N-1>::value};
template <typename T>
static inline void set(T& tup, int l_item)
{
cout<<"item value: "<<l_item<<", N-1: "<< N-1 << ", value: "<<value <<endl;
typedef typename boost::tuples::element<N-1, T>::type _el_type;
get<N-1>(tup) = atomic_item<N-1, _el_type>(l_item);
};
};
template<>
struct GEN<0>
{
enum {value = 0};
template <typename T>
static inline void set(T& tup, int l_item)
{
typedef typename boost::tuples::element<0, T>::type _el_type;
get<0>(tup) = atomic_item<0, _el_type>(l_item);
};
};
main(){
....
/** this is how i try to invoke it */
GEN<3>::set(w,1);
}
输出:
项目值:1,N-1:2,值:0
函数仅被调用一次...
编辑
有没有办法用 for_each 或其他任何东西进行循环以获得类似的东西:
for_each<range_c<int,0,3> f{operator()(T i)GEN<typename T::value>::set(w,1)}>
或者类似的东西来实现所有这些元素的调用?
我特别想要这个:
GEN<3>::set(w,1);
GEN<2>::set(w,1);
GEN<1>::set(w,1);
In循环。
I try to invoke function void set (...) recursively using metaprogramming.
The problem is that it seems to invokes only once.
template <int N>
struct GEN
{
enum {value = GEN<N-1>::value};
template <typename T>
static inline void set(T& tup, int l_item)
{
cout<<"item value: "<<l_item<<", N-1: "<< N-1 << ", value: "<<value <<endl;
typedef typename boost::tuples::element<N-1, T>::type _el_type;
get<N-1>(tup) = atomic_item<N-1, _el_type>(l_item);
};
};
template<>
struct GEN<0>
{
enum {value = 0};
template <typename T>
static inline void set(T& tup, int l_item)
{
typedef typename boost::tuples::element<0, T>::type _el_type;
get<0>(tup) = atomic_item<0, _el_type>(l_item);
};
};
main(){
....
/** this is how i try to invoke it */
GEN<3>::set(w,1);
}
Output:
item value: 1, N-1: 2, value: 0
function has been invoked only once...
EDIT
is there a way to do kind of loop with for_each or anything else to get something simmilar:
for_each<range_c<int,0,3> f{operator()(T i)GEN<typename T::value>::set(w,1)}>
or something similar to achieve invoke for all of those elements?
Particularly I'd like to have this:
GEN<3>::set(w,1);
GEN<2>::set(w,1);
GEN<1>::set(w,1);
In loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有递归。递归意味着调用自己。代码中的
set
函数不会执行此操作。您的
value
声明确实递归(即GEN::value
是根据GEN定义的: :value
) – 但以一种非常无趣的方式,它只是传播基本情况值 0 – 而且你似乎并没有使用这个值。/编辑:这是一个非常简单的示例来解决您在评论中提出的观点,即实现
实际上非常简单的效果:
现在您可以通过
GEN<3>::set(w, 1)
并且它将具有所需的语义。There is no recursion. Recursion means calling yourself. The
set
function in your code does not do this.Your
value
declaration does recurse (i.e.GEN<N>::value
is defined in terms ofGEN<N -1>::value
) – but in a pretty uninteresting way, it just propagates the base case value, 0 – and furthermore you don’t seem to be using this value anyway./EDIT: Here’s a very simple example to address the point raised by you in the comments, i.e. to achieve the effect of
That’s actually pretty easy:
Now you can call this code via
GEN<3>::set(w, 1)
and it will have the desired semantics.自从您的代码编译并运行以来,元编程模板显然确实递归了。
您是否期望递归调用
set
函数?您调用的函数GEN<3>::set
不会调用任何其他类的set
函数,因此不存在运行时递归。只有编译时递归来实例化模板。但编译时递归不会在运行时生成输出。The metaprogramming template obviously did recurse since your code compiled and ran.
Were you expecting a recursive call of the
set
function? The function you called,GEN<3>::set
doesn't call theset
function of any other class, so there is no run-time recursion. There is only compile-time recursion to instantiate the templates. But compile-time recursion doesn't generate output at run time.