Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: I think references should have been const by default Date: Mon, 25 Oct 2021 10:56:24 -0700 Organization: None to speak of Lines: 34 Message-ID: <877de12dkn.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="ce9c6a8e39b023180b9e14b2203de6ab"; logging-data="1558"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18dq1nVpgSCzNjj6l6Ak1o8" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:0amXZRajQbOQs7fBSlU98WdqK7I= sha1:DKRKL+ReF2BQXbkXtYpYdM4adc4= Xref: csiph.com comp.lang.c++:82081 James Kuyper writes: > On 10/25/21 5:47 AM, Juha Nieminen wrote: > ... >> Most modern C compilers will give you a warning if you try to assign >> a const char* (eg. a string literal) to a non-const char* (or give >> one to a function taking a non-const char*), > > They must do so on assignment; 6.5.16p2 occurs in a "Constraints" section: > "An assignment operator shall have a modifiable lvalue as its left operand." > > And if a function prototype is in scope "... the arguments are > implicitly converted, as if by assignment, to the types of the > corresponding parameters ..." (6.5.2.2p7), so the same constraints apply > there, too. For the same reason, they also apply to return statements. I think the example being referred to was something like: char *s; s = "hello"; which does not require a diagnostic in C (because C string literals are not const). The following is recommended in C and required in C++: const char *s; s = "hello"; but here s is still a modifiable lvalue because the "const" applies to what s points to, not to s itself. A case that would invoke the constraint in 6.5.16p2 is: char *const s; s = "hello"; because s itself is read-only; you can't assign *anything* to it. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */