Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: James Kuyper Newsgroups: comp.lang.c Subject: Re: Naming conventions (was Re: valgrind leak I can't find) Date: Fri, 23 Aug 2024 11:57:20 -0400 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: <1c5db75fd1fd3b0e61bd2517a38f2829d0aeee6c.camel@tilde.green> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 23 Aug 2024 18:07:42 +0200 (CEST) Injection-Info: dont-email.me; posting-host="79844fb89d55e8dec0eecfb9e2d937eb"; logging-data="1032897"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/21KZa8HT6v8BqIXFE/toGit0rkHVggxk=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:U2O9Wdm8q7uoBK/L46Dr7tYHPPA= Content-Language: en-US In-Reply-To: Bytes: 3623 On 22.08.2024 16:01, Thiago Adams wrote: > > C++ also made the use of "struct/union/enum" before tags optional. > > For example: > > struct X x; > > In C++, we can write: > > X x; > > Consequence? That's possible because C defines separate name spaces of identifiers (not to be confused with C++ namespaces) for labels, tags, each struct or union type, and attributes; all other identifiers are in the ordinary name space. Because of the relevant grammar rules, names in the different spaces can never occur in locations where it is ambiguous as to which name space the identifier is from. Therefore, C allows you do use the same identifier in different name spaces in the same scope with different meanings. For example, identifiers in the tag name space are always prefixed with struct, union or enum, so they can never be confused with names from the ordinary name space. In C++, there are separate name spaces only for macros and labels. All other identifiers are in the same name space, including tags. Class, struct, and union members are not controlled by name spaces, but by scope rules that are different from those in C. That's what allows you to use a tag without a preceding "class", "struct", or "union" keyword. Note: The name space for macros isn't really comparable to the other name spaces I've mentioned, which is why C doesn't have such a name space. Macros are replaced with their expansions during translation phase 3, whereas the other name spaces only become meaningful during translation phase 8. If C were changed to allow use of tags without "struct" or "union" before them, the tag name space would have to be merged into the ordinary name space, and that would break all kinds of legacy code that uses the same identifier as a tag and as an identifier in the ordinary name space. That would be a backwards incompatible change, and the committee tends to avoid such changes. I once spent some time creating an example program that could be compiled in either C or C++, that displayed every construct that had defined behavior in both C and C++, but had incompatibly different behavior in the two languages. Most of the examples involved name spaces. Most of the other differences have at least unspecified behavior in one of the two languages; more often they are syntax errors or constraint violations in the wrong language.