在C ++中自动生成代码 [英] Automatic code generation in C++

查看:230
本文介绍了在C ++中自动生成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一段不涉及循环但自动生成一些C ++代码的代码。

I want a piece of code which does not involve loops but automatically generates some C++ code.

我有一个 const int d ,从这里我想写入d行代码访问数组。例如

I have an const int d, and from this I want to write d lines of code to access an array. So for instance

for(int k=0; k<d;++k){
  // do something to myarryay[k];
}

但我不想在for循环中写。我想要编译器执行,如下面的代码行:

but I don't want to write this in a for loop. I want the complier to execute as if the following lines of code was written:

do something to myarray[0]
do something to myarray[1]
.
.
.
do something to myarray[d]

任何人都可以给我一些建议做这个?

Can anyone give me a suggestion on some code that does this?

提前感谢。

推荐答案

您确定需要手动进行吗?这是一种称为循环展开的优化。在足够高的优化级别,你的编译器会为你做,并且可能更好,因为一个好的优化编译器将考虑权衡(减少指令缓存局部性,一个)。

Are you sure you need to do this manually? This is an optimization known as loop unrolling. At high enough optimization levels, your compiler will do it for you, and possibly better than you can, since a good optimizing compiler will take into account the tradeoffs (reduced instruction cache locality, for one).

这篇关于在C ++中自动生成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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