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: Fri, 22 Oct 2021 12:04:56 -0700 Organization: None to speak of Lines: 36 Message-ID: <87v91o3mp3.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="5689f352f8227660374b3a0a6a945f72"; logging-data="11569"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197fCElKcd9/JKMZzf4WDj6" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:Gi3wVE58bhsIiziX6Fflr6NJtbI= sha1:TyCfzpPQoGWSqK4HeivOgbHIDCk= Xref: csiph.com comp.lang.c++:82039 Bart writes: [...] > That program only behaves as I said when I use my own compiler. > > With gcc and others that support _Generic, any 'const' attributes of > the types of controlling expressions appear to be stripped away. > > I'm not sure why that is. It looked to be a bug in gcc, but other > compilers do the same. Maybe they are all copying gcc's behaviour, but > I don't what C itself says about it. [...] _Generic is a C feature that does not appear in C++. The first operand of _Generic is an expression, not an object, and it determines the type of that expression. If the expression happens to be an lvalue, it undergoes *lvalue conversion* (N1570 6.3.2.1p2), which strips any qualifiers. So if the operand of _Generic is the name of an object of type const int, the type of that *expression* is int, not const int. (Something like _Generic that operates on lvalues rather than expressions might have been useful, but I've never had a need for it.) Consider an expression like (n + 1). Would you expect it to have a different type depending on whether n was defined as const or not? C++ overloading, unlike C's _Generic, can use references to distinguish between const and non-const objects. You can't have two overloaded functions that differ only in their parameter type, int vs. const int, but you can have overloaded functions with parameters of type int& and const int&. -- 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 */