5 ms·
overlooks the value of const qualification for the caller - if the argument is marked as const, the caller might reasonably assume the data won’t be changed.
by foxhill 7y ago
overlooks the value of const qualification for the caller - if the argument is marked as const, the caller might reasonably assume the data won’t be changed.
- deleted 7y ago[deleted]
- vortico 7y agoYes, there are many cases where the caller can optimize the dependency chain / order of constFoo(x); constBar(x); where it couldn't make that assumption if one function wasn't const.
- mlyle 7y agoUnfortunately, C++ const functions are not necessarily pure functions and cannot be freely reordered.
- vortico 7y agoYou're right, I was mistaken. And if the function is in the same compilation unit so the compiler is able to prove that it's pure, it would also be able to prove that a non-const pointer is actually const.
- commandlinefan 7y agoI don't think he's saying that you shouldn't use const (if he is, I vehemently disagree!) just that using it won't make the code faster. It will definitely prevent you, or the next maintenance programmer, from making a lot of dumb mistakes.