Topic: Reserved identifiers in C++ vs C?
Author: "eli.friedman@gmail.com" <eli.friedman@gmail.com>
Date: Sun, 2 Aug 2009 01:43:18 CST Raw View
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-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Sun, 2 Aug 2009 20:39:35 CST Raw View
eli.friedman@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.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Sun, 2 Aug 2009 20:38:26 CST Raw View
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.kanze@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
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: usenet@mkarcher.dialup.fu-berlin.de (Michael Karcher)
Date: Mon, 3 Aug 2009 11:30:41 CST Raw View
eli.friedman@gmail.com <eli.friedman@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
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]