Does declaring C++ variables const help or hurt performance?

后端 未结 5 1205
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 04:45

I understand the behavior of const-qualified data types. I am curious, though, if there is any performance gain or loss from over- or under-zealousness of quali

5条回答
  •  情话喂你
    2020-12-19 05:22

    const is just there to help you catch errors at compile time. However since there's this thing called the const_cast you can always change the constness of any variable so the compiler really can't get away with optimizing anything away. (You can also have fun with c-style casts to get rid of constness which could make optimizations invalid.)

提交回复
热议问题