为什么默认情况下不是C ++类型const? [英] Why aren't C++ types const by default?

查看:110
本文介绍了为什么默认情况下不是C ++类型const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gurus无处不在告诉我们 const 一切,除非我们需要修改它,但标准使得一切可变,直到我们声明 const

Gurus everywhere tell us to const everything unless we need to modify it, yet the standard makes everything mutable until we declare it const.

我缺少这里的东西,还是这是一个矛盾?为什么默认情况下不是C ++类型 const ,根据专家(其中一些人可能设计的标准),他们应该 code> const 默认情况下?

Am I missing something here, or is this a contradiction? Why aren't C++ types const by default when, according to the experts (some of whom presumably designed the standard), they should be made const by default?

推荐答案


告诉我们const一切,除非我们需要修改它。

Gurus everywhere tell us to const everything unless we need to modify it,

这是传统的智慧十多二十年,是的。 >

That's been conventional wisdom for a decade or two, yes.


但是标准使得一切都变得可变,直到我们声明它const。

yet the standard makes everything mutable until we declare it const.


$ b b

标准是从五十年前开发的语言演变而来的,其中const正确性,甚至类型检查,大多被认为只是学术兴趣。

The standard has evolved from languages developed around fifty years ago, when const-correctness, and even type checking, were largely regarded as being only of academic interest.


我在这里缺少什么,或者这是一个矛盾?

Am I missing something here, or is this a contradiction?

这不是矛盾;只是一种语言,不工作如何一些人会说它应该。你仍然可以(而且应该)在适当的时候声明 const 的东西,我们只要忍受一种不会推动我们走向安全实践的语言。

It's not a contradiction; just a language that doesn't work how some would say it should. You still can (and should) declare things const where appropriate, and we just have to put up with a language that doesn't push us towards safe practices.


为什么默认情况下不是C ++类型 const

因为改变语言的这个基本方面将会破坏所有现有的代码。这是一个更大的关注,而不是稍微减少错误的范围。

Because changing such a fundamental aspect of the language will break just about all existing code. That's a much bigger concern than slightly reducing the scope for mistakes.

这篇关于为什么默认情况下不是C ++类型const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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