Path: csiph.com!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: Tue, 26 Oct 2021 09:49:34 -0700 Organization: None to speak of Lines: 39 Message-ID: <87y26f20kh.fsf@nosuchdomain.example.com> References: <87v91jzzgt.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="b46c3160cab81c3625e1c265f36f8f38"; logging-data="9602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5Kn/V7d/Zf+vRD9zltflG" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:khtnm8u6asctCZGM1/QEi8ZZ4bs= sha1:sIbLBZRhhXgZaGq50qGHepRcBfs= Xref: csiph.com comp.lang.c++:82117 Bart writes: > On 26/10/2021 14:29, Ben Bacarisse wrote: >> Bart writes: [...] >>> char* s = "ABC"; >> This relies on an a conversion that is valid (bad unwise) in C and >> not >> permitted in C++. > > I tried it in C++ before posting (as I'd thought that "ABC" would have > type const char*) but it seemed to work. (Using -Wall -std=c++14.) And you didn't bother to mention the diagnostic? I get warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] And of course with "-pedantic-errors" it becomes a fatal error. Did you not get a diagnostic? It's not all that interesting to see what you can get away with by ignoring warnings. > I'm writing about what is typically observed. What I typically observe is that skilled programmers pay attention to warnings and do not attempt to modify string literals. > (I don't put string literals into a readonly segment because I haven't > got round to it yet. > > It is surprising that a big compiler like MSVC doesn't do so either, > but apparently that's only done when optimising; rather odd.) My quick experiment with MSVC 2017 does not confirm that. char *s = "ABC"; gives a fatal error in C++. It compiles in C (as expected), but attempting to modify the literal causes a run-time crash. -- 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 */