Topic: underscores in names


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/29
Raw View
In article <36370F1F.799EAD4D@dera.gov.uk>, David Bruce
<dib@dera.gov.uk> writes
>Uh, just curious, but *why* is no diagnostic required?  I mean, (IMO, at
>least) normally it is a good idea to report programmer errors, especially
>when they're easy to detect.  Is this case really that much harder to detect
>than the above quote suggests?

My guess is that it was just inherited from C.  It has always deeply
irritated me that compilers do not diagnose invasions into reserved
territory.  Of course the problem is that those same compilers have to
compile the implementors' code.

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/10/29
Raw View
David Bruce wrote:
>
> Stan Brown wrote:
>
> > "In addition, identifiers containing a double underscore (_ _) or
> > beginning with an underscore and an upper-case letter are reserved for
> > use by C++ implementations and shall not be used otherwise; no diagnostic
> > is required." -- CD2, sec 2.10 para 2
> >
> > That last clause ("no diagnostic") means that if you use a name from the
> > forbidden categories, a conforming compiler is not obligated to issue any
> > warning or error messages; it's allowed to just do the wrong thing.
>
> Uh, just curious, but *why* is no diagnostic required?  I mean, (IMO, at
> least) normally it is a good idea to report programmer errors, especially
> when they're easy to detect.  Is this case really that much harder to detect
> than the above quote suggests?
>
>   --  David

The reason it's reserved is that the implementation might use it. In
particular, the implementation could use such identifiers in the
definition of a macro or an inline function in one of the standard
headers. When your code makes use of one of those macros or functions,
it could be difficult for the compiler to recognise that those symbols
come from the implementation's code rather than from your code. It's not
impossible to make the distinction, but isn't necessarily trivial,
either.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/10/29
Raw View
In article <3642af6e.1150449393@netnews.worldnet.att.net>,
jackklein@worldnet.att.net says...

[ ... ]

> > Thus, double underscores make it a reserved identifier only if they are
> > at the beginning. Was this a change from C89?
>
> <Jack>
>
> No this is not a change from C89.

Sorry Jack, but this is _absolutely_ wrong.

> The only place underscores have ever been
> restricted in C is at the beginning.  A variable named v______v is and has
> always been perfectly legal.

In C++, a two underscores in a row ANYWHERE in an identifier makes
that a reserved name.  You canNOT use the identifier you gave above in
a well formed C++ program.

This IS a change from C, which reserves names that start with an
underscore followed by either another underscore, OR a capital letter.
UNlike C++, C does NOT reserve identifiers that have two (or more)
underscores elsewhere in the names.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/10/30
Raw View
David Bruce <dib@dera.gov.uk> writes:

> Stan Brown wrote:

>> That last clause ("no diagnostic") means that if you use a name from the
>> forbidden categories, a conforming compiler is not obligated to issue any
>> warning or error messages; it's allowed to just do the wrong thing.

> Uh, just curious, but *why* is no diagnostic required?

So that the compiler implementation can use those names in its own
include-files, and the compiler code needs not be modified to
recognize those particular names every time the include-files are
modified.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:oliva@gnu.org mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/10/30
Raw View
Jerry Coffin wrote:
>
> In article <3642af6e.1150449393@netnews.worldnet.att.net>,
> jackklein@worldnet.att.net says...
>
> [ ... ]
>
[I wrote, Re:C9X]
> > > Thus, double underscores make it a reserved identifier only if they are
> > > at the beginning. Was this a change from C89?
> >
> > <Jack>
> >
> > No this is not a change from C89.
>
> Sorry Jack, but this is _absolutely_ wrong.

Jack is saying here that the C9X rule is not a change from the C89 rule.

> > The only place underscores have ever been
> > restricted in C is at the beginning.  A variable named v______v is and has
> > always been perfectly legal.

Which is the rule that I described for C9X, and which Jack confirmed was
not a change from the rule for C89.

> In C++, a two underscores in a row ANYWHERE in an identifier makes
> that a reserved name.  You canNOT use the identifier you gave above in
> a well formed C++ program.

Jack wasn't talking about C++. I was responding to contradict a claim
that the C rule was the same as the C++ rule.

> This IS a change from C, which reserves names that start with an
> underscore followed by either another underscore, OR a capital letter.
> UNlike C++, C does NOT reserve identifiers that have two (or more)
> underscores elsewhere in the names.

So we're in perfect agreement on the facts.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/28
Raw View
Nathan Myers wrote:
>
> To get an idea of the effect of this, take a look at the latest
> SGI STL, from http://www.sgi.com/Technology/STL/.  The names have
> been "uglified" so that any idiotic-but-permitted macro your program
> defines won't break the STL.  Now, strictly speaking the SGI STL is
> now non-conforming -- unless your implementer provides it to you.
> We hope that in the future that will always happen.

Yeah, Borland did that to their C library at one point, too, i.e.,

 void* memset(void*__s, int __c, size_t __n);

It makes one wish for something like:

 #pushandhidealldefinesexcept()
 // library definitions
 #popalldefines

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jackklein@worldnet.att.net (Jack Klein)
Date: 1998/10/28
Raw View
On 27 Oct 98 14:18:27 GMT, James Kuyper <kuyper@wizard.net> wrote:

> Stan Brown wrote:

> > geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo) wrote:

> > >I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
> > >that not only are variable names not supposed to start with underscores,
> > >but that they're not supposed to have double underscores anywhere in the
> > >names.

> > You might have read it on my Web page too! <grin> (Well, maybe not:
> > that's the rules for C, which are not quite the same as C++ rules.)

> My copy of the draft C9X standard says in section 7.1.3:

> | All identifiers that begin with an underscore and either an uppercase
> | letter or another underscore are always reserved for any use.
> | All identifiers that begin with an underscore are always reserved for
> | use as identifiers with file scope in both the ordinary and tag name
> | spaces.

> Thus, double underscores make it a reserved identifier only if they are
> at the beginning. Was this a change from C89?

<Jack>

No this is not a change from C89.  The only place underscores have ever been
restricted in C is at the beginning.  A variable named v______v is and has
always been perfectly legal.

</Jack>



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/28
Raw View
James Kuyper wrote:

[...]

> > >Could someone tell me why it was necessary to disallow double underscores
> > >within names? Why isn't it sufficient to disallow names that start with
> > >an underscore?
>
> Possibly it was decided that C++ implementations would be more complex
> than C implementations, and hence would need a larger reserved name
> space?

Thy definitively need a larger reserved name space, for name mangling.

Try to compile the following on g++:

int f__Fv;

void f() {}


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David Bruce <dib@dera.gov.uk>
Date: 1998/10/29
Raw View
Stan Brown wrote:

> "In addition, identifiers containing a double underscore (_ _) or
> beginning with an underscore and an upper-case letter are reserved for
> use by C++ implementations and shall not be used otherwise; no diagnostic
> is required." -- CD2, sec 2.10 para 2
>
> That last clause ("no diagnostic") means that if you use a name from the
> forbidden categories, a conforming compiler is not obligated to issue any
> warning or error messages; it's allowed to just do the wrong thing.

Uh, just curious, but *why* is no diagnostic required?  I mean, (IMO, at
least) normally it is a good idea to report programmer errors, especially
when they're easy to detect.  Is this case really that much harder to detect
than the above quote suggests?

  --  David
________________________________________________________________________
post: DERA Malvern, St Andrews Road, Malvern, WORCS WR14 3PS, ENGLAND
mailto:dib@dera.gov.uk ** phone: +44 1684 895112 ** fax: +44 1684 894389
[The views expressed above are entirely those of the writer and do not represent the views, policy or understanding of any other person or official body.]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo)
Date: 1998/10/26
Raw View
I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
that not only are variable names not supposed to start with underscores,
but that they're not supposed to have double underscores anywhere in the
names.

For example, "_abc" is not allowed and "abc__def" is not allowed.

I couldn't find this in the standard document, so I'd appreciate it if
someone would point me to where it says this. Also, I'd like to know if
this also disallows triple underscores (and quadruple, etc) in variable
names. I know triple underscores contain double underscores, but it's
possible that the standard might be worded such that triple underscores
are allowed.

Could someone tell me why it was necessary to disallow double underscores
within names? Why isn't it sufficient to disallow names that start with
an underscore?



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: brownsta@concentric.net (Stan Brown)
Date: 1998/10/26
Raw View
[posted and emailed]

geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo) wrote:

>I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
>that not only are variable names not supposed to start with underscores,
>but that they're not supposed to have double underscores anywhere in the
>names.

You might have read it on my Web page too! <grin> (Well, maybe not:
that's the rules for C, which are not quite the same as C++ rules.)

>For example, "_abc" is not allowed and "abc__def" is not allowed.

Your second example is correct; your first is not. A user program is
allowed to define an identifier _abc (underscore followed by lower-case
letter or digit) but not _ABC (underscore followed by upper-case letter).

>I couldn't find this in the standard document, so I'd appreciate it if
>someone would point me to where it says this.

In the index under "reserved identifiers", on the first try I found:

"In addition, identifiers containing a double underscore (_ _) or
beginning with an underscore and an upper-case letter are reserved for
use by C++ implementations and shall not be used otherwise; no diagnostic
is required." -- CD2, sec 2.10 para 2

That last clause ("no diagnostic") means that if you use a name from the
forbidden categories, a conforming compiler is not obligated to issue any
warning or error messages; it's allowed to just do the wrong thing.

>Also, I'd like to know if
>this also disallows triple underscores (and quadruple, etc) in variable
>names. I know triple underscores contain double underscores,

So you've answered your own question.

>Could someone tell me why it was necessary to disallow double underscores
>within names? Why isn't it sufficient to disallow names that start with
>an underscore?

There had to be a clear set of possible identifiers that could legally be
used in standard header files and for special purposes by compilers or
compiler-supplied libraries, so that programmers would know that they
were free to use all other identifiers.

Even though you legally *can* define your own names _abc or _34, I
suggest you don't. I've been told that some compilers mistakenly assume
that all identifiers beginning with an underscore are reserved to the
implementation, so you might have a name collision with a compiler
designed in that incorrect way.

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                      http://www.concentric.net/%7eBrownsta/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/10/26
Raw View
geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo) writes:

>I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
>that not only are variable names not supposed to start with underscores,
>but that they're not supposed to have double underscores anywhere in the
>names.

>For example, "_abc" is not allowed and "abc__def" is not allowed.

>I couldn't find this in the standard document,

Section 17.4.3.1 "Reserved names" and its subsections contains a
complete discussion of what names are reserved and in what contexts.
The discussion is too long to quote here.

"Reserved" means reserved to the C++ implementation for its
own uses. You risk unpredictable behavior if you create your
own identifiers that violate the restrictions.

Names with a single leading underscore not followed by an upper-case
letter are allowed in some contexts, but are reserved for use as
global identifiers. As a practical matter, parts of the implementation
other than the compiler itself, like operating-system header files,
often use leading underscores for many purposes, so you are well-
advised not to create your own names with leading underscores
for any purpose.

Any name containing a double underscore is reserved to the C++
implementation for all uses.  If a name contains a triple or
quadruple underscore, it certainly contains a double underscore,
and so is reserved.

>Could someone tell me why it was necessary to disallow double underscores
>within names?

The restriction allows implementations to use double underscores
in name mangling and be sure that no mangled name can collide with
any user-defined un-mangled name. It hardly seems necessary (or
even desirable) to use double underscores in your own identifiers,
so the impact of the restriction was judged to be very minor.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/10/26
Raw View
Stan Brown<brownsta@concentric.net> wrote:
>geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo) wrote:
>
>>I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
>>that not only are variable names not supposed to start with underscores,
>>but that they're not supposed to have double underscores anywhere in the
>>names.
>> ....
>>Could someone tell me why it was necessary to disallow double underscores
>>within names? Why isn't it sufficient to disallow names that start with
>>an underscore?
>
>There had to be a clear set of possible identifiers that could legally be
>used in standard header files and for special purposes by compilers or
>compiler-supplied libraries, so that programmers would know that they
>were free to use all other identifiers.

To get an idea of the effect of this, take a look at the latest
SGI STL, from http://www.sgi.com/Technology/STL/.  The names have
been "uglified" so that any idiotic-but-permitted macro your program
defines won't break the STL.  Now, strictly speaking the SGI STL is
now non-conforming -- unless your implementer provides it to you.
We hope that in the future that will always happen.

Another implementer use for "__" in names is that some compilers
mangle argument type names into function names using "__" as a
delimiter.  Putting such sequences into either type names or
function names could break such a mangling scheme.  (The SGI STL
has some functions which begin with "__"; this use doesn't seem
to break anybody's mangling scheme, yet.)

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/27
Raw View
In article <MPG.109e590e96c696d1989680@chnews.intel.com>, Geoff Fortytwo
<geoff.d.fortytwo@REMOVETHIS.intel.com> writes
>Could someone tell me why it was necessary to disallow double underscores
>within names? Why isn't it sufficient to disallow names that start with
>an underscore?

Implementors need some support for the name mangling algorithms that
most of them use to provide type safety.  Name mangling is simply a
method by which the simple name you write is extended to add data about
scope and type.  Scope information is needed to allow distinction
between the same identifier spelling being selected by a programmer in
different scopes, type information is needed to support overloading.
While the standard does not require name mangling to solve the problem
it does recognise that it is a common and effective solution to the
problem which requires some support to prevent a user from accidentally
writing a name that looks like a mangled one.  BTW this probably means
that you should never use any multiple number of contiguous underscores
in a name.

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1998/10/27
Raw View
Stan Brown wrote:
>
> [posted and emailed]
>
> geoff.d.fortytwo@REMOVETHIS.intel.com (Geoff Fortytwo) wrote:
>
> >I read somewhere (I think it was Dr. Dobbs or The C/C++ User's Journal)
> >that not only are variable names not supposed to start with underscores,
> >but that they're not supposed to have double underscores anywhere in the
> >names.
>
> You might have read it on my Web page too! <grin> (Well, maybe not:
> that's the rules for C, which are not quite the same as C++ rules.)

My copy of the draft C9X standard says in section 7.1.3:

| All identifiers that begin with an underscore and either an uppercase
| letter or another underscore are always reserved for any use.
| All identifiers that begin with an underscore are always reserved for
| use as identifiers with file scope in both the ordinary and tag name
| spaces.

Thus, double underscores make it a reserved identifier only if they are
at the beginning. Was this a change from C89?

> >For example, "_abc" is not allowed and "abc__def" is not allowed.
>
> Your second example is correct; your first is not. A user program is
> allowed to define an identifier _abc (underscore followed by lower-case
> letter or digit) but not _ABC (underscore followed by upper-case letter).
>
> >I couldn't find this in the standard document, so I'd appreciate it if
> >someone would point me to where it says this.
>
> In the index under "reserved identifiers", on the first try I found:
>
> "In addition, identifiers containing a double underscore (_ _) or
> beginning with an underscore and an upper-case letter are reserved for
> use by C++ implementations and shall not be used otherwise; no diagnostic
> is required." -- CD2, sec 2.10 para 2

The final version of the C++ standard has that wording moved to section
17.4.3.1.2.

> >Could someone tell me why it was necessary to disallow double underscores
> >within names? Why isn't it sufficient to disallow names that start with
> >an underscore?

Possibly it was decided that C++ implementations would be more complex
than C implementations, and hence would need a larger reserved name
space?

> There had to be a clear set of possible identifiers that could legally be
> used in standard header files and for special purposes by compilers or
> compiler-supplied libraries, so that programmers would know that they
> were free to use all other identifiers.

That explains why there are reserved identifiers, but not why they
needed such a large set.

> Even though you legally *can* define your own names _abc or _34, I
> suggest you don't. I've been told that some compilers mistakenly assume
> that all identifiers beginning with an underscore are reserved to the
> implementation, so you might have a name collision with a compiler
> designed in that incorrect way.

That's particularly likely if they share components with a C
implementation.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]