元编程模板递归不递归(编辑重要)

发布于 2024-10-11 20:57:55 字数 1369 浏览 3 评论 0原文


我尝试使用元编程递归调用函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-10-18 20:57:55

没有递归。递归意味着调用自己。代码中的 set 函数不会执行此操作。

您的 value 声明确实递归(即 GEN::value 是根据 GEN定义的: :value) – 但以一种非常无趣的方式,它只是传播基本情况值 0 – 而且你似乎并没有使用这个值。

/编辑:这是一个非常简单的示例来解决您在评论中提出的观点,即实现

GEN<3>::set(w,1);
GEN<2>::set(w,1);
GEN<1>::set(w,1);

实际上非常简单的效果:

template <unsigned N>
struct GEN {
    template <typename T>
    static void set(T& w, int value) {
        // Do something, e.g.:
        get<N - 1>(w) = value;
        // Recurse:
        GEN<N - 1>::set(w, value);
    }
};

template <>
struct GEN<0> {
    template <typename T>
    static void set(T&, int) { /* empty */ }
};

现在您可以通过 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 of GEN<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

GEN<3>::set(w,1);
GEN<2>::set(w,1);
GEN<1>::set(w,1);

That’s actually pretty easy:

template <unsigned N>
struct GEN {
    template <typename T>
    static void set(T& w, int value) {
        // Do something, e.g.:
        get<N - 1>(w) = value;
        // Recurse:
        GEN<N - 1>::set(w, value);
    }
};

template <>
struct GEN<0> {
    template <typename T>
    static void set(T&, int) { /* empty */ }
};

Now you can call this code via GEN<3>::set(w, 1) and it will have the desired semantics.

情感失落者 2024-10-18 20:57:55

自从您的代码编译并运行以来,元编程模板显然确实递归了。

您是否期望递归调用 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 the set 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文