5 ms·
Can you please provide some examples where this really becomes a problem, so I can better understand what you mean?
by golergka 11y ago
Can you please provide some examples where this really becomes a problem, so I can better understand what you mean?
- jblow 11y agoEverybody who has tried to use const in even medium-sized C++ codebases is familiar with the behavior discussed here. It's not about specific examples, it is about what happens when you have a lot of code.
- golergka 11y agoCan you please provide some examples, so I can better understand what you mean?
- trashcan 11y agoIf you make a function that accepts an unsigned int, and then you try to pass in a const unsigned int, you will need to either typecast it, make a new function, or change the function to only accept const unsigned ints. Although it's been a long time since I coded in C/C++, correct me if I'm wrong.
- Joky 11y agoThat sounds completely wrong, unless the function takes a reference to the unsigned int.
- StephanTLavavej 11y agoYou're incorrect, for C89 through C++17. I'd recommend not making statements about languages you've forgotten. In the C++ Working Paper, the Standardese is N4527 5.2.2 [expr.call]/4 "When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument." followed by 8.5 [dcl.init] where /17.8 has a nice example: "Note: An expression of type "cv1 T" can initialize an object of type "cv2 T" independently of the cv-qualifiers cv1 and cv2. int a; const int b = a; int c = b;" (A "cv-qualifier" is const or volatile; they behave similarly in many contexts, hence the collective terminology for convenience.)
- trashcan 11y agoI stand corrected, thank you.
- agumonkey 11y agoI'm no C nor Ada expert, but I think he means in C `const` is a data type modifier, whereas in Ada it's a function modifier, making the concerns orthogonal. I'm never happy when I see warning about `const` being lifted by some function in C. I think that's what parent is referring to.
- Negitivefrags 11y agoI'm someone with a codebase where we practice const correctness and we haven't had a problem with it. We were being const correct from the start so we don't have the problems associated with trying to change that midstream. Assuming you do it from the start you shouldn't be running in to issues.
- mark-r 11y agoExactly right. But the example was of combining two code bases, one of which tried to maintain const correctness and one which had utter disregard for it. If you don't believe in const correctness in the first place, the work of making your code compatible can seem intractable.
- fsloth 11y agoHmm, I'm not sure if codebase with millions of lines is 'medium' or 'large' but from my experience const is great even there. Of course, modifying existing code to become const correct in interfaces where previously it was not can cascade to a point where a majority of the code needs a rewrite, so I can see situations where adding const to an old interface can cause lots of grief. But from my experience const really helps in enforcing directed graph constraints on logic and data flow which is great - and forces code to be easier to reason about.
- angersock 11y agoConst, in theory, is a fine idea. As implemented in C++ though, it's unwieldy. There's at least three (if memory serves) places you can put it in a member function declaration (not including params). What's more, it can "infect" any other code that touches it, in turn requiring casting or weird const additions. For a simple rant, check the first few paragraphs here: http://www.vaxpower.org/~isildur/const.html http://www.vaxpower.org/~isildur/const.html
- Joky 11y agoLooks like someone who does not even understand C++ in the first place...
- krapp 11y agoWhat the author there is calling "infecting" and "mangling" seems to be const doing exactly what it's intended to do. You can't just declare something a constant then cast it out of being constant later? Of course not, that's the entire point!
- angersock 11y agoNo, see, that's the thing. It's annoying when you want to--and I apologize because it's been several years since I had to deal with this nonsense--copy elements between logically compatible containers but ones whose types are differing in layers of constness. Or if you to, say, move a reference all the way down into a pointer (or vice versa) to interop with C or other legacy code. Or any of several other things, including some truly odd possibilities using operator and function overloading. It's just really gross as implemented.
- CountSessine 11y agocopy elements between logically compatible containers but ones whose types are differing in layers of constness. Or if you to, say, move a reference all the way down into a pointer (or vice versa) to interop with C or other legacy code. Or any of several other things, including some truly odd possibilities using operator and function overloading. And this is 'const''s fault? Others have asked - can you provide an example? Because what you're describing sounds like it's going to be a hot mess, const or no const. I don't really think that const can make bad data representation any worse than it already is.