Reserved identifiers in C++ vs C?

74 views
Skip to first unread message

eli.fr...@gmail.com

unread,
Aug 2, 2009, 3:43:18 AM8/2/09
to
The C++ standard says that "Each name that contains a double
underscore _ _ or begins with an underscore followed by an uppercase
letter (2.11) is reserved to the implementation for any use." The
equivalent section of C99 says "All identifiers that begin with an
underscore and either an uppercase letter or another underscore are
always reserved for any use." Is it intentional that using
identifiers like kw__for (with two underscores) is legal in C but not C
++? If so, why?

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Francis Glassborow

unread,
Aug 2, 2009, 10:39:35 PM8/2/09
to
eli.fr...@gmail.com wrote:
> The C++ standard says that "Each name that contains a double
> underscore _ _ or begins with an underscore followed by an uppercase
> letter (2.11) is reserved to the implementation for any use." The
> equivalent section of C99 says "All identifiers that begin with an
> underscore and either an uppercase letter or another underscore are
> always reserved for any use." Is it intentional that using
> identifiers like kw__for (with two underscores) is legal in C but not C
> ++? If so, why?

The original motivation behind reserving all uses of __ to the
implementation was to provide a mechanism for name mangling (i.e.
encoding the function signature in the name used by the compiler) While
there are other ways that name mangling (or more generally overloading
of function names) could have been supported the choice was motivated by
the need to support code translators (e.g. CFront)that converted C++
code to C and so needed to keep withing the character set supported by C.
Changing that rulle would require a rewrite of sections of most current
C++ implementations to no great gain.

In practice __ is rarely used in C and where it is a C++ compiler will
normally be able to cope with it.

James Kanze

unread,
Aug 2, 2009, 10:38:26 PM8/2/09
to
On Aug 2, 9:43 am, "eli.fried...@gmail.com" <eli.fried...@gmail.com>
wrote:

> The C++ standard says that "Each name that contains a double
> underscore _ _ or begins with an underscore followed by an
> uppercase letter (2.11) is reserved to the implementation for
> any use." The equivalent section of C99 says "All identifiers
> that begin with an underscore and either an uppercase letter
> or another underscore are always reserved for any use." Is it
> intentional that using identifiers like kw__for (with two
> underscores) is legal in C but not C ++?

Yes.

> If so, why?

Historical reasons. Some early C++ compilers used C as an
intermediate language. Internally generated symbols used double
underscore, which is legal in C; by forbidding it in C++, they
ensure that these symbols can never conflict with a user defined
symbol.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34

Michael Karcher

unread,
Aug 3, 2009, 1:30:41 PM8/3/09
to
eli.fr...@gmail.com <eli.fr...@gmail.com> wrote:
> always reserved for any use." Is it intentional that using
> identifiers like kw__for (with two underscores) is legal in C but not C
> ++? If so, why?
If it is intentional, I suppose that is to have a namespace for
C++-to-C-translators like Cfront to use for mangled names.

Regards,
Michael Karcher

Reply all
Reply to author
Forward
0 new messages