Sujet : Re: More Funny Stuff From C
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.os.linux.advocacyDate : 12. Jun 2024, 04:43:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4b1vq$1evff$3@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Pan/0.158 (Avdiivka; )
On 11 Jun 2024 18:23:28 GMT, rbowman wrote:
... I've hit instances using functions
in legacy code where one const in a function resulted in having to
spread const around to keep the compiler from whining.
Dennis Ritchie himself pointed out some pitfalls with the “const” concept
early on. For example, take this standard library routine:
char *strstr(const char *haystack, const char *needle);
Note that the result is a (non-const) pointer into one of the arguments.
But the arguments are const! So the function is not typesafe. It was done
this way so that, if the user passed a pointer into a modifiable string,
they could get back a pointer they could use to make further
modifications.
The right solution would have been to create two versions of this routine,
one with a non-const haystack pointer returning non-const, and another
with a const haystack pointer, returning const.