Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: my solution for Pascal strings Date: Thu, 26 Mar 2015 11:33:33 -0700 Organization: None to speak of Lines: 80 Message-ID: <87fv8robgi.fsf@kst-u.example.com> References: <52a5b8b7-8c35-4a96-a438-bf173a427e95@googlegroups.com> <0cd4a139-c466-4c04-95cc-f4436be0034c@googlegroups.com> <011mw.191541$812.115671@fx20.am4> <2a3a2358-7726-4c79-9e46-3bfd565253bc@googlegroups.com> <20141223141323.231@kylheku.com> <1wnmw.309290$td6.28533@fx30.am4> <3db88ab4-eecb-468d-8cb1-c659bbac140d@googlegroups.com> <87sicrojir.fsf@kst-u.example.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="c77ff4d35e33d1c939d3461ebde8dd0d"; logging-data="11018"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7CSSgvH3fH4Bbagf5AG8N" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:uYq2FUx0v0v0k1Xo0tahkZuf8Pg= sha1:WypgvJI363Fi/7RfClOI5NoMxP8= Xref: csiph.com comp.lang.c:59037 Bartc writes: > On 26/03/2015 15:39, Keith Thompson wrote: >> Bartc writes: >> [...] >>> I don't like 'const' as used in C, where it doesn't really define a >>> constant, but suggests that a variable is read-only. >> >> Yes, that's exactly what const means. Would you like it better if it >> were spelled "readonly"? > > Yes. > > Although I would still never use it and it would probably increase the > clutter because it's longer. Which means you have no way to enforce that something must not be modified, and you risk bugs where a function modifies something that you expected to be unchanging. Ok, what about "ro"? Or, as I suggested, nothing, with a keyword like "var" to indicate that an object *can* be modified? > With 'const' ideally meaning what a lot of people already mistakenly > think it means (ie. what it signifies in Pascal). Having a keyword that means that something is evaluated at compile time might be useful. I think that's what C++'s "constexpr" does. But that and read-only are two different concepts -- both useful IMHO. >> [...] >> >>> As far as I'm concerned, 'const' is just extra clutter. And some >>> people like to stick it in nearly everywhere. >> >> In my experience, people don't use it enough. I see code that defines >> and initializes an object and then never changes it, If it had been >> defined with "const", I would *know* that it still has its initial >> value (without looking for code that modifies it). > > No, there are sometimes so many consts littering the code > that you /can't/ easily see what's what! I haven't found that to be the case. >>> And that leads to type conflicts. So if you pass a pointer to that >>> const int to a function that takes 'int*', then you'll get a type >>> warning. You'd have to change the parameter type to 'const int*'. This >>> is the start of the 'const poisoning' that Malcolm is on about. >> >> If the function doesn't modify the object the pointer points to, the >> parameter probably should have been defined as "const int*" in the first >> place. > > Exactly. And if that parameter is then an argument to another function > belonging to a library, then /that/ now needs 'const'. I think this is > what is meant by const poisoning: you have a huge application and a > set of libraries that have worked quite happily now, until someone > innocently writes: > > const int n=5; > > and you need to pass &n. Now you have to revise the entire code base > to remove all the warnings. Yes, taking existing code that doesn't use "const" and modifying it to use "const" consistently can be messy. Writing code that uses "const" properly in the first place can avoid that mess. If you've defined const int n=5; then you don't want n to change, ever. Why would you want to pass its address to a function that doesn't promise not to modify it? -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"