const(C ++)的用处 [英] Usefulness of const (C++)

查看:128
本文介绍了const(C ++)的用处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试着看看const和non const的各种反汇编输出版本的函数,我还没有看到一个显着的改进。我假设编译器现在能够做非常聪明的事情与非const函数,在技术上可以是const。



还有一些情况下const是有用的机器级别?任何例子?

解决方案

据我所知,标记一个函数const的唯一效果是允许函数被调用在 const 对象。没有优化的好处。



Herb Sutter有一篇文章深入讨论了const和优化:





const 应用于数据 - const数据可能可以放在不可写内存中。


I'm a const fiend, and I strive to make everything as const as possible.

I've tried looking at various dissassembly outputs from const and non const versions of functions, and I've yet to see a marked improvement however. I'm assuming compilers nowadays are able to do smart things with non const functions that could technically be const.

Are there still cases where const is useful at the machine level? Any examples?

解决方案

As far as I know, the only effect of marking a function const is to allow the function to be called on a const object. There's no optimization benefit.

Herb Sutter has an article which discusses const and optimization in depth:

The one area that const is useful at the machine level is when applied to data - const data might be able to be placed in non-writable memory.

这篇关于const(C ++)的用处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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