C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining names with leading underscores

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Keith A. Lewis
Guest





PostPosted: Sun Jan 30, 2005 10:59 am    Post subject: Defining names with leading underscores Reply with quote



Do header files with inclusion guards like:

#ifndef _name
#define _name
....
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Sun Jan 30, 2005 11:47 pm    Post subject: Re: Defining names with leading underscores Reply with quote



Keith A. Lewis wrote:
Quote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

I think it is permissible so long as no standard headers are included
afterwards (since they may use such names) but it would be hard to
ensure this ordering.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrew Peter Marlow
Guest





PostPosted: Sun Jan 30, 2005 11:50 pm    Post subject: Re: Defining names with leading underscores Reply with quote



On Sun, 30 Jan 2005 05:59:17 -0500, Keith A. Lewis wrote:

Quote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

I don't know is the short answer. But it doesn't matter if it applies to
macro names or not. If one defines a macro with a leading underscore then
section 17.4.3.1.2 means your macro might replace a name that is reserved
by the implementation, so wrong things will still happen. That is why,
IMO, the restriction should be considered to apply to macro names as well
as identifier names. The use of a leading underscore for any identifier is
forbidden by the coding guidelines I use. And I have seen code fail to
compile in some environments where this rule has been broken.

-apm



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Sun Jan 30, 2005 11:52 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Keith A. Lewis wrote:
Quote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

The name of a macro is a name. 17.4.3.1.2 imposes constraints on names.

I've never understood this urge to write names that start with
underscores. Just don't do it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Sun Jan 30, 2005 11:53 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Keith A. Lewis wrote:
Quote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

It does not apply to macro names.

Two consecutive underscores or an initial underscore followed by a capital
letter are reservered EVERYWHERE.

Leading underscores followed by lowercase letters are permitted execpt in
idenifiers in the global namespace. That is for example:

char* _name;

would be prohibited.

Frankly, my include guards look like:

#if !INCLUDED_FILE_H
#define INCLUDED_FILE_H 1

#endif

no doubt what we're doing.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Sun Jan 30, 2005 11:54 pm    Post subject: Re: Defining names with leading underscores Reply with quote

On 30 Jan 2005 05:59:17 -0500, [email]kal (AT) panix (DOT) com[/email] (Keith A. Lewis) wrote in
comp.lang.c++.moderated:

Quote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

The section you sited quite clearly begins with "each name", and makes
no exceptions for macro names, so I don't know why there would be any
question about their being exempt. Note that macro names are always
effectively in the global namespace, since they are handled in an
earlier translation phase than namespace definitions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Krügler (ne Spange
Guest





PostPosted: Mon Jan 31, 2005 11:44 am    Post subject: Re: Defining names with leading underscores Reply with quote

Hello Pete Becker,

Pete Becker schrieb:

Quote:
The name of a macro is a name. 17.4.3.1.2 imposes constraints on names.

I've never understood this urge to write names that start with
underscores. Just don't do it.

True that is. Although, I understand the wish to introduce symbols like


_1

in lambda expressions. (Btw: 17.4.3.1.2, 2nd rule allows them for
**names** in non-global
namespaces and if they became standardized, they are also ok)

Greetings from Bremen,

Daniel


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Mon Jan 31, 2005 8:10 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Jack Klein wrote:

Quote:
The section you sited quite clearly begins with "each name", and makes
no exceptions for macro names, so I don't know why there would be any
question about their being exempt. Note that macro names are always
effectively in the global namespace, since they are handled in an
earlier translation phase than namespace definitions.

Because a macro name isn't in the global namespace. A name in a local
scope hides the one in the global namespace to, but either way they aren't
IN the global namespace.

_lower is ONLY prohibitted as symbols in the global namespace, not locals,
not within a class, and NOT MACROS.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
msalters
Guest





PostPosted: Mon Jan 31, 2005 8:14 pm    Post subject: Re: Defining names with leading underscores Reply with quote


Jack Klein wrote:
Quote:
On 30 Jan 2005 05:59:17 -0500, [email]kal (AT) panix (DOT) com[/email] (Keith A. Lewis) wrote
in
comp.lang.c++.moderated:

Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to
define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

The section you sited quite clearly begins with "each name", and
makes
no exceptions for macro names, so I don't know why there would be any
question about their being exempt. Note that macro names are always
effectively in the global namespace, since they are handled in an
earlier translation phase than namespace definitions.

There are four kinds of clashes: Your name [macro or namespace]
with an implementation-defined [macro or namespace].

Now, for each of these possible collisions there are in effect
specific rules:

your macro vs. implementation-defined macro:
-->implementation must use __ or _[A-Z] to protect its macro, _a is
not reserved for the implementation in this context.

your macro vs. implementation-defined name in :: or ::std::
--> implementation can use _a, but only if at least one std header
is included. This also forbids the definition of a macro _a /after/
the header is included. This is probably non-obvious, see note.

your name vs implementation-defined macro:
--> implementation must use __ or _[A-Z] to protect its macro, which
can clobber your name independent of namespaces (e.g. it could turn
the token into a } )

your name vs implementation-defined name in :: or ::std::
--> implementation can use _a, in global scope or in std::. You may
use _a in other scopes, without conflict, but not in global scope
even if you don't include a header.

Note:
These rules are based on 17.4.3.1.2, but also take into account the
phases of translation. In particular, the preprocessor cannot
determine what is and is not at global scope. However, the standard
headers may include some non-standard constructs which enforce
some rules. E.g. they could include a part #pragma include-bottom
which is inserted at the end of a TU. The rules in 17.4.3.1.2 also
apply to such constructs, when they say a name is reserved. This
is why you can't define _a in these cases.

On the other hand, if you don't include such headers, the TU
contains a well-defined set of statements which must have
well-defined behavior. The standard has enough wording which
makes it clear an implementation can't include its own headers
at will (but if you include one, you might get all of them).
It but can predefine macros (like __FILE__). This means
you can create a TU in which you do control the names in the
global namespace, e.g. _a. Now, it would be an ODR violation
if you defined another _a, but macros don't cause ODR violations.
Regards,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Mon Jan 31, 2005 8:16 pm    Post subject: Re: Defining names with leading underscores Reply with quote

I wrote:
Quote:
Keith A. Lewis wrote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

I think it is permissible so long as no standard headers are included
afterwards (since they may use such names) but it would be hard to
ensure this ordering.

I was mistaken; any translation unit that includes a standard header
and defines such a macro, even afterwards, is unportable. Paragraph
17.4.3.1.1/2 says:

"A translation unit that includes a header shall not contain any
macros that define names declared or defined in that header."

Practically, an implementation may define macros whose expansions use
such names, e.g. <assert.h> may define the assert macro to expand to a
call to a function named ::_assert. Defining a macro called _assert
after including <assert.h> would break the assert macro.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Tue Feb 01, 2005 9:21 am    Post subject: Re: Defining names with leading underscores Reply with quote

On 30 Jan 2005 18:53:17 -0500, Ron Natalie <ron (AT) sensor (DOT) com> wrote in
comp.lang.c++.moderated:

Quote:
Keith A. Lewis wrote:
Do header files with inclusion guards like:

#ifndef _name
#define _name
...
#endif

violate the C++ standard? In particular, is it permissible to define
macro names with leading underscores? Section 17.4.3.1.2
[lib.global.names] seems to forbid this, but my question is whether
or not this also applies to macro names.

It does not apply to macro names.

Where does the normative wording of the standard state that it does
not apply to macro names?

Quote:
Two consecutive underscores or an initial underscore followed by a capital
letter are reservered EVERYWHERE.

Leading underscores followed by lowercase letters are permitted execpt in
idenifiers in the global namespace. That is for example:

I think there is some confusion here caused by two specific practices
in the C++ standard, both in fact changes from the C standard.

1. The rather loose usage of the terms "name" and "identifier".

2. The elimination of the C term "file scope", since this term does
not exist in the C++ standard.

16.3 p6 makes this much clear:

"The identifier immediately following the define is called the macro
name."

Also consider the definition of the term identifier (2.10), which does
not exclude macros, and which specifically refers to the originally
mentioned 17.4.3.1.2 specifically. So even though the latter category
only speaks of "names", it makes it clear that "name" is a synonym for
"identifier".

Finally, 3.3.5 P3 states:

"A name declared outside all named or unnamed namespaces (7.3), blocks
(6.3), function declarations (8.3.5), function definitions (8.4) and
classes (clause 9) has global namespace scope (also called global
scope). The potential scope of such a name begins at its point of
declaration (3.3.1) and ends at the end of the translation unit that
is its declarative region. Names declared in the global namespace
scope are said to be global."

Now since a macro has a name, and that name is defined outside all
named or unnamed namespaces (to the preprocessor, everything is in the
global namespace), macro names are in the global namespace.

Quote:
char* _name;

would be prohibited.

Frankly, my include guards look like:

#if !INCLUDED_FILE_H
#define INCLUDED_FILE_H 1

#endif

no doubt what we're doing.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Tue Feb 01, 2005 8:05 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Jack Kl."ein wrote:

Quote:

Also consider the definition of the term identifier (2.10), which does
not exclude macros, and which specifically refers to the originally
mentioned 17.4.3.1.2 specifically. So even though the latter category
only speaks of "names", it makes it clear that "name" is a synonym for
"identifier".

I disagree. If your interpretation is correct, the standard is inconsistant.
Macros aren't part of any "declartive region", so they can not be part of the
global (or any other ) namespace.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Tue Feb 01, 2005 8:05 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Ben Hutchings wrote:

Quote:

"A translation unit that includes a header shall not contain any
macros that define names declared or defined in that header."

Practically, an implementation may define macros whose expansions use
such names, e.g. <assert.h> may define the assert macro to expand to a
call to a function named ::_assert. Defining a macro called _assert
after including <assert.h> would break the assert macro.

I disagree with this interpretaion. The rule is to prevent you from

making macros that map to functions that the STANDARD documents as being
declared.

For example: You can't #define vector 42 if you include <vector>

How could you possibly enforce the rule the way you define it? There
are no bounds on things a header "might" declare/define.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Tue Feb 01, 2005 8:05 pm    Post subject: Re: Defining names with leading underscores Reply with quote

msalters wrote:

Quote:
your macro vs. implementation-defined name in :: or ::std::
--> implementation can use _a, but only if at least one std header
is included. This also forbids the definition of a macro _a /after/
the header is included. This is probably non-obvious, see note.

Huh? There's no restriction on _a as a macro name regardless of
what headers have been defined. Standard headers must use one of
the reserved identifiers (two underscores or _Upper) to protect
against such a collision. It's non-obvious because it's not true.


Quote:
Note:
These rules are based on 17.4.3.1.2, but also take into account the
phases of translation. In particular, the preprocessor cannot
determine what is and is not at global scope.

So?

Quote:
However, the standard
headers may include some non-standard constructs which enforce
some rules. E.g. they could include a part #pragma include-bottom
which is inserted at the end of a TU. The rules in 17.4.3.1.2 also
apply to such constructs, when they say a name is reserved. This
is why you can't define _a in these cases.

Huh? _a isn't reserved for macros. I can put

#define _a 1

No matter where you look in the phases of translation, _a is NOT
in the global namespace.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Feb 02, 2005 8:11 pm    Post subject: Re: Defining names with leading underscores Reply with quote

Ron Natalie wrote:
Quote:
Ben Hutchings wrote:


"A translation unit that includes a header shall not contain any
macros that define names declared or defined in that header."

Practically, an implementation may define macros whose expansions use
such names, e.g. <assert.h> may define the assert macro to expand to a
call to a function named ::_assert. Defining a macro called _assert
after including <assert.h> would break the assert macro.

I disagree with this interpretaion. The rule is to prevent you from
making macros that map to functions that the STANDARD documents as being
declared.

I'm not so sure.

Quote:
For example: You can't #define vector 42 if you include <vector

How could you possibly enforce the rule the way you define it? There
are no bounds on things a header "might" declare/define.

Yes there are; they can only define standard names and reserved names
(the latter only in the scopes in which they are reserved).

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.