const 对优化器有帮助吗? C++

发布于 2024-09-26 14:34:35 字数 298 浏览 2 评论 0原文

可能的重复:
C++ 中的常量和编译器优化

让圣战开始吧: 关于 const 在 C++ 中的用处,我听到了许多不同的意见。当然它在成员函数声明等中有用。但是它作为变量(或者更确切地说,常量)的修饰符有多大用处呢?如果其余代码保持不变,它确实对优化器有帮助吗?

Possible Duplicate:
Constants and compiler optimization in C++

Let the holy wars begin:
I've heard a number of differing opinions on the usefulness of const in C++. Of course it has uses in member function declarations, etc. But how useful is it as a modifier on variables (or rather, constants)? Does it indeed help the optimizer, if the rest of the code is left the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

伏妖词 2024-10-03 14:34:35

在很多情况下,const 修饰符对优化器没有帮助,因为编译器已经可以判断您是否修改了变量。在我看来,const 的最大好处是它告诉编译器程序员是否打算修改该变量,这对于在编译时而不是运行时查找某些类型的语义错误非常有用。任何可以转移到编译时的错误都会极大地提高程序员的生产力。

There are a lot of cases where the const modifier won't help the optimizer, for the simple fact that the compiler can already tell if you modified a variable or not. The biggest benefit of const, in my opinion, is that it tells the compiler whether the programmer intended to modify that variable, which is useful in finding certain types of semantic errors at compile time instead of run time. Any error you can move to compile time is a huge boost in programmer productivity.

残龙傲雪 2024-10-03 14:34:35

const 对优化器没有帮助。

由于 const 可以用 const_cast 抛弃,因此可以编写在多个地方使用 const 的程序,然后抛弃它并修改无论如何,变量都具有根据标准定义的行为。因此,编译器必须查看程序的实际代码来确定何时修改哪些变量,并且无论如何它可能非常擅长(例如,它可能会确定非常量变量在某个代码块上是不变的,并相应地进行优化)。

如果编译器盲目地将 const 视为某些内容不会改变的保证,优化器就会破坏一些格式良好的程序。

const 是一个编译时功能,通过添加一些编译时约束并指示代码契约(例如“我保证不会更改此参数”)来帮助程序员编写正确的代码。它与优化无关。虽然不变量对于优化器很重要,但这与 const 关键字无关。

有一个例外:使用 const 声明的对象。这些不能修改;即使它们是通过强制转换实现的,其行为也是未定义的。这里有一些微妙之处:

const int ci = 5;
const_cast<int&>(ci) = 5; // undefined behavior, original object declared const

int i = 5;
const int& ci2 = i;        // cannot modify i through ci2, const reference
const_cast<int&>(ci2) = 5; // OK, original object not declared const

因此,当编译器看到 const int ci 时,它可能会认为它永远不会改变,因为修改它是未定义的行为。然而,这很可能不是程序的瓶颈,它只是一个更复杂的#define。除此之外,const 很弱——只是类型系统的一个关键字。

const does not help the optimizer.

Since const can be cast away with const_cast, it's possible to write programs that use const in a number of places, then cast it away and modify variables anyway, with defined behavior according to the standard. The compiler therefore must look at the actual code of the program to determine which variables are modified when, and it's probably pretty good at this anyway (for example it might determine a non-const variable is invariant over a certain block of code and optimize accordingly).

If the compiler blindly treated const as a guarantee that something won't change, the optimizer would break some well-formed programs.

const is a compile-time feature to help programmers write correct code, by adding some compile-time constraints, and indicating a code contract (eg. 'I promise not to change this parameter'). It has nothing to do with optimization. While invariants are important to optimizers, this has nothing to do with the const keyword.

There is one exception: objects declared with const. These cannot be modified; even if they are via casting, the behavior is undefined. There's some subtlety here:

const int ci = 5;
const_cast<int&>(ci) = 5; // undefined behavior, original object declared const

int i = 5;
const int& ci2 = i;        // cannot modify i through ci2, const reference
const_cast<int&>(ci2) = 5; // OK, original object not declared const

So when the compiler sees const int ci it probably does assume it will never, ever change, because modifying it is undefined behavior. However, chances are this isn't the bottleneck in your program, it's just a more sophisticated #define. Apart from that, const is weak - just a keyword for the type system.

七禾 2024-10-03 14:34:35

一般来说,不,它不会对编译器有帮助。由于在 C 和 C++ 中常量性可以在一秒钟内被抛弃,因此编译器很难对满足优化的代码要求做出必要的假设。

也就是说,常量正确性应该始终用于其他好处。

In general, no, it will not help the compiler. Since the const-ness can be casted away in a second in both C and C++, it'd be hard for the compiler to make the necessary assumptions about fulfilled code requirements for optimizations.

That said, const-correctness should always be used for its other benefits.

她说她爱他 2024-10-03 14:34:35

它不会造成伤害,理论上可以允许一些优化,所以你不妨使用它 - 不知道是否有任何生产编译器这样做。

It can't hurt and in theory could allow for some optimizations so you might as well use it - don't know if any production compilers do.

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