是否存在向局部变量添加 const 限定符可能会引入运行时错误的情况?
这是我多次执行过的(诚然是脑死亡的)重构算法:
- 从一个可以干净编译并且(AFAICT)正常工作的
.cpp
文件开始。 - 通读该文件,只要有未使用
const
关键字声明的本地/堆栈变量,请将const
关键字添加到其声明中。 - 再次编译
.cpp
文件 - 如果报告任何新的编译时错误,请检查相关代码行以确定原因 - 如果事实证明本地变量确实需要是非< code>const,从中删除
const
关键字;否则修复const
添加关键字所揭示的任何潜在问题。 - 转到 (3) 直到 .cpp 文件再次干净地编译
暂时搁置“const 所有局部变量”是否是一个好主意,是否有任何问题这种做法是否有将运行时/逻辑错误引入到程序中而在编译时无法捕获的风险? AFAICT 这看起来“安全”,因为它不会引入回归,只会引入编译时错误,然后我可以立即修复这些错误;但 C++ 是一个非常辉煌的东西,所以也许存在一些我没有想到的风险。
Here's an (admittedly brain-dead) refactoring algorithm I've performed on several occasions:
- Start with a
.cpp
file that compiles cleanly and (AFAICT) works correctly. - Read through the file, and wherever there is a local/stack-variable declared without the
const
keyword, prepend theconst
keyword to its declaration. - Compile the
.cpp
file again - If any fresh compile-time errors are reported, examine the relevant lines of code to determine why -- if it turns out the local-variable legitimately does need to be non-
const
, remove theconst
keyword from it; otherwise fix whatever underlying issue theconst
keyword's addition has revealed. - Goto (3) until the
.cpp
file again compiles cleanly
Setting aside for the moment whether or not it's a good idea to "const all the local variables", is there any risk of this practice introducing a run-time/logic error into the program that wouldn't be caught at compile-time? AFAICT this seems "safe" in that it won't introduce regressions, only compile-time errors which I can then fix right away; but C++ is a many-splendored thing so perhaps there is some risk I haven't thought of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意接受一个人为的示例,您可能会进入未定义行为的世界。
上面的编译并输出 100。下面的编译并允许做任何它想做的事情(但碰巧为我输出 99)。 通过非常量访问路径修改 const 对象会导致未定义的行为。
是的,这是人为的,因为为什么有人会对非 const 对象执行 const_cast ?另一方面,这是一个简单的例子。也许在更复杂的代码中这实际上可能会出现。 耸耸肩我不会说这是一个很大的风险,但它确实属于“任何风险”,如问题中所述。
If you're willing to accept a contrived example, you could enter the world of undefined behavior.
The above compiles and outputs 100. The below compiles and is allowed to do whatever it wants (but happened to output 99 for me). Modifying a const object through a non-const access path results in undefined behavior.
Yes, this is contrived because why would someone do a
const_cast
on a non-const
object? On the other hand, this is a simple example. Maybe in more complex code this might actually come up. Shrug I won't claim that this is a big risk, but it does fall under "any risk", as stated in the question.将 const 添加到变量可能会导致调用不同的重载:
https://godbolt.org/z/aE3jzPjP3< /a>
将
var
更改为 const 将导致程序调用不同的函数,从而引发异常。它可以很容易地触发完全不同的行为:
Adding const to a variable can cause a different overload to be called:
https://godbolt.org/z/aE3jzPjP3
Changing
var
to const will cause the program to call a different function and thereby throw an exception.It can easily trigger completely different behaviour:
const
没有运行时影响,编译器不会在编译时告诉您并导致构建失败。const
has no runtime implications, that the compiler will not tell you at compile time and fail the build.