5 ms·
'const' in C and C++ is broken. Not only is it like a Chinese word that varies wildly in meaning depending on where you use it and how it is inflected, but in C
by bitwize 11y ago
'const' in C and C++ is broken. Not only is it like a Chinese word that varies wildly in meaning depending on where you use it and how it is inflected, but in C-family languages, 'const' modifies type, making objects that benefit from the compiler hints 'const' implies type-incompatible with objects that do not. This results in the problem of some know-it-all deciding that a core API needs to be const-correct, and checks in changes making it so, only to find that the entire fucking code base is now broken.
Note that I'm not saying "don't use 'const'" with the above (and I'm CERTAINLY not saying "using '#define const' in your code base is a good idea"); only that 'const' in C-family languages has significant drawbacks which may affect the engineering tradeoffs you make when you decide whether to use it.
By contrast, languages such as Ada have explicit and implicit constantness restrictions ('in' parameters, for instance, cannot be modified), but these do not modify the type of the object, allowing objects with these restrictions to be type-compatible with objects without them.
- chimeracoder 11y ago> only that 'const' in C-family languages has significant drawbacks AFAIK this is pretty much limited to C and C++. Many (and I am willing to venture most, or at least the most-used) C-family languages that aren't C/C++ don't suffer this issue. Go and Javascript, for example, both use "const" in a way that doesn't modify the type of the underlying object.
- bitwize 11y ago"C-family languages" here means C, C++, Objective-C and D -- not any of the "look-like-C-but-have-completely-different-semantics" languages, or Go which is probably closer to proposed C replacement Alef than to C itself.
- golergka 11y agoI think that the scope of this discussion is limited to C/C++. If a programmer doesn't like immutability as a broad concept, he is clearly an incompetent imbecile (and I'm trying to write that as polite as possible, given the severity of the issue).
- golergka 11y agoCan 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.)
- CountSessine 11y agoI don't really understand what you're saying. 'const' in C and C++ is broken. Not only is it like a Chinese word that varies wildly in meaning depending on where you use it and how it is inflected, but in C-family languages, 'const' modifies type, making objects that benefit from the compiler hints 'const' implies type-incompatible with objects that do not. They're not type-incompatible in any way that doesn't violate those compiler hints in the first place - C and C++ fully support sensible coercion between const and non-const types. What more do you want? This results in the problem of some know-it-all deciding that a core API needs to be const-correct, and checks in changes making it so, only to find that the entire fucking code base is now broken Would you rather it compiled and silently broke? If some dingus breaks the build by changing an API without checking in the corresponding code using the API, how is that C/C++'s fault? By contrast, languages such as Ada have explicit and implicit constantness restrictions ('in' parameters, for instance, cannot be modified), but these do not modify the type of the object, allowing objects with these restrictions to be type-compatible with objects without them. This is bizarre. Can you take that 'in' parameter and then pass it on to another function as an 'in/out' parameter? If so, then either 1. Ada just made you a copy silently (bad) 2. Ada is broken and 'in' means nothing (worse). If neither of those are true and an 'in' can't be transitively passed on as an 'in/out', then that's a type - however you want to refer to it, 'in' is then a type with all of the 'broken' type restrictions you're railing against in C/C++.
- thisisnotatest 11y agoconst is type-incompatible like this: Suppose you want to write a function that returns all the nodes in a tree that satisfy some property. In C++ you have to define two different versions (or use templates to make the compiler clone it for you): vector<Node*> GetGoodNodes(Node* root); vector<const Node*> GetGoodNodes(const Node* root); If more than one type is involved in the operation, there can be a combinatorial explosion.
- golergka 11y agoI don't feel that I understand purpose of this function yet. Does this function return data point that it creates itself, or does it retrieve these nodes from somewhere else? If it's the first option, who's in charge of deleting them? If it's a second option, who's in charge of counting references to these objects? Also, if it's a second option, and this data points are retrieved from a certain storage, are you absolutely sure that whoever gets this data should be able to modify them?
- lisivka 11y ago> but in C-family languages, 'const' modifies type, making objects that benefit from the compiler hints 'const' implies type-incompatible with objects that do not. It was fixed years ago. Now const behaves properly in modern C compilers.
- TwoBit 11y agoDude you are clueless. I hope you aren't in the position of making technical decisions for any major C/C++ projects.