Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Differences between C and C++ Date: Mon, 25 Jul 2022 12:21:21 -0700 Organization: None to speak of Lines: 33 Message-ID: <87r129yp5a.fsf@nosuchdomain.example.com> References: <87k081tbjm.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="535dfeeb34a6b5be91014b647da5c16e"; logging-data="1491425"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19H704HvyAwvINvDD2jzWLW" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:CaO07lP6IizvrMWeUdKiXAGkaPI= sha1:K908/421gyrGLn/jgulgRRo6b5c= Xref: csiph.com comp.lang.c++:85627 Muttley@dastardlyhq.com writes: > On Mon, 25 Jul 2022 17:14:05 +0100 > Ben Bacarisse wrote: [...] >>Some things that are compound literals in C /are/ valid in C++. For >>example: >> >> struct s { int v; }; >> ... >> f((struct s){1}); >> >>is ok in both, but add a pointer and you get the same difference as >>above: >> >> g(&(struct s){1}); // ok in C, not ok in C++ > > IOW mostly contorted syntax that was probably never intended to be used but > happens to be legal due to the way the C parser works. Not at all. In C, `(struct s){1}` is a compound literal, and it's an lvalue so applying `&` to it is perfectly valid. There's nothing accidental about it. C++ doesn't have compound literals, but it has some other features (that C doesn't) that make `f((struct s){1})` valid, but in C++ it's not an lvalue, so `&(struct s){1}` is not valid. -- 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 */