编译时间循环 [英] compile time loops

查看:116
本文介绍了编译时间循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可能有编译时循环。

例如,我有以下模板类:

  template< class C,int T = 10,int B = 10> 
class CountSketch
{
public:
CountSketch()
{
hashfuncs [0] =& CountSketch< C> :: hash< 0> ;
hashfuncs [1] =& CountSketch< C> :: hash< 1> ;;
// ... for all i until i == T在编译时已知
};
private:
template< int offset>
size_t hash(C& c)
{
return(reinterpret_cast< int>(& c)+ offset)
}
size_t(CountSketch :: * hashfuncs [T])(C& c);
};

我想知道我是否可以使用循环来初始化T散列函数。循环的边界在编译时是已知的,所以,原则上,我没有看到任何原因,为什么它不能做(特别是如果我手动展开循环,它的工作原理)。



当然,在这个具体的例子中,我可以做一个带有2个参数的单一哈希函数(尽管我的猜测效率会降低)。因此,我对解决这个特定问题不感兴趣,而是知道对于类似的情况是否存在编译时循环。



谢谢! / p>

解决方案

不,不是直接可能的。模板元编程是一种纯功能语言。通过它定义的每个值或类型都是不可变的。一个循环本质上需要可变变量(重复测试一些条件,直到X发生,然后退出循环)。



递归。 (每次使用不同的模板参数来实例化此模板,直到达到一些终止条件)。



但是,这可以解决所有与循环相同的问题。 / p>

编辑:这里有一个简单的例子,在编译时使用递归计算N的阶乘:

  template< int N> 
struct fac {
enum {value = N * fac< N-1> :: value};
};

模板<>
struct fac< 0> {
enum {value = 1};
};

int main(){
assert(fac< 4> :: value == 24);
}

C ++中的模板元编程是一种图灵完备的语言,不要碰到各种内部编译器限制,你可以基本上解决它的任何问题。



然而,出于实际目的,它可能值得调查像Boost.MPL ,其中包含大量的数据结构和算法,可以简化大量的元编程任务。


I would like to know if it is possible to have sort of compile time loops.
For example, I have the following templated class:

template<class C, int T=10, int B=10>
class CountSketch
{
public:
    CountSketch()
    {   
         hashfuncs[0] = &CountSketch<C>::hash<0>;
         hashfuncs[1] = &CountSketch<C>::hash<1>;
         // ... for all i until i==T which is known at compile time
    };
private:
    template<int offset>
    size_t hash(C &c)
    {
        return (reinterpret_cast<int>(&c)+offset)%B;
    }
    size_t (CountSketch::*hashfuncs[T])(C &c);
};

I would thus like to know if I can do a loop to initialize the T hash functions using a loop. The bounds of the loops are known at compile time, so, in principle, I don't see any reason why it couldn't be done (especially since it works if I unroll the loop manually).

Of course, in this specific example, I could just have made a single hash function with 2 parameters (although it would be less efficient I guess). I am thus not interested in solving this specific problem, but rather knowing if "compile time loops" existed for similar cases.

Thanks!

解决方案

Nope, it's not directly possible. Template metaprogramming is a pure functional language. Every value or type defined through it are immutable. A loop inherently requires mutable variables (Repeatedly test some condition until X happens, then exit the loop).

Instead, you would typically rely on recursion. (Instantiate this template with a different template parameter each time, until you reach some terminating condition).

However, that can solve all the same problems as a loop could.

Edit: Here's a quick example, computing the factorial of N using recursion at compile-time:

template <int N>
struct fac {
  enum { value = N * fac<N-1>::value };
};

template <>
struct fac<0> {
  enum { value = 1 };
};

int main() {
  assert(fac<4>::value == 24);
}

Template metaprogramming in C++ is a Turing-complete language, so as long as you don't run into various internal compiler limits, you can solve basically any problem with it.

However, for practical purposes, it may be worth investigating libraries like Boost.MPL, which contains a large number of data structures and algorithms which simplify a lot of metaprogramming tasks.

这篇关于编译时间循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆