compile time loops

前端 未结 5 1819
时光说笑
时光说笑 2020-12-01 06:51

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

        
5条回答
  •  失恋的感觉
    2020-12-01 07:07

    There are compilers that will see the loop and unroll it. But it's not part of the language specification that it must be done (and, in fact, the language specification throws all sorts of barriers in the way of doing it), and there's no guarantee that it will be done, in a particular case, even on a compiler that "knows how".

    There are a few languages that explicitly do this, but they are highly specialized.

    (BTW, there's no guarantee that the "unrolled" version of your initializations would be done "at compile time" in a reasonably efficient fashion. But most compilers will, when not compiling to a debug target.)

提交回复
热议问题