ES.25: Declare an object const or constexpr unless you want to modify its value later on
ES.25:如果没有考虑修改对象的值,就将对象定义为const或者constexpr
Reason(原因)
That way you can't change the value by mistake. That way may offer the compiler optimization opportunities.
这么做之后,就不会发生因为错误而修改变量值的情况。这种方式还增加了编译优化的机会。
Example(示例)
Enforcement(实施建议)
Look to see if a variable is actually mutated, and flag it if not. Unfortunately, it may be impossible to detect when a non-const was not intended to vary (vs when it merely did not vary).
观察变量是否可变,如果不是就标记它。不幸的是,可能很难检测什么时候一个非常量没有修改的意图(或者它仅仅是还没有修改)。
原文链接
觉得本文有帮助?欢迎点赞并分享给更多人。
阅读更多更新文章,请关注微信公众号【面向对象思考】
















