reserved identifiers?

T

Tim H

I know C's rules about reserved identifiers (anything starting with
"_", leading "is", etc).

I don't know C++ rules. I assume the same rules and more apply?

I've seen several conventions for member variables:

foo_
m_foo
_foo

In the case of member variables are _ prefixes still reserved?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I know C's rules about reserved identifiers (anything starting with
"_", leading "is", etc).

I don't know C++ rules. I assume the same rules and more apply?

I've seen several conventions for member variables:

foo_
m_foo
_foo

In the case of member variables are _ prefixes still reserved?

Any name beginning with a double underscore or an underscore followed
by an uppercase letter is reserved for any use, in short, don't use.
Any name beginning with an underscore is reserved for use in the
global namespace, meaning be careful.

Personally I put an underscore at the end of the names of members but
I'm sure you can find a lot of other practices, all (probably) equally
good as long as they don't do something illegal.
 
G

Greg Herlihy

That's not correct according to 17.3.4.1.2/2:

Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.

In other words, names that begin with a leading underscore and that are not
in the global namespace are available as names for the program to use. And
since the names of member variables are names in the scope of the class that
defines them - they are not names in the global namespace itself. So a
program is free to use a name with a (single) leading underscore as the name
of a class member. Nevertheless, to keep things simple it is probably a good
idea for a program not to use a name with a leading underscore anywhere -
even when it may be OK to do so.

Greg
 
G

Gavin Deane

In other words, names that begin with a leading underscore and that are not
in the global namespace are available as names for the program to use. And
since the names of member variables are names in the scope of the class that
defines them - they are not names in the global namespace itself. So a
program is free to use a name with a (single) leading underscore as the name
of a class member.

Not quite. As Erik Wikström says elsethread, names beginning with a
leading underscore followed by an uppercase letter are reserved for
the implementation for any use. So, Because member variables are not
in the global namespace, _Member is an illegal name for a member
variable whereas _member is not. _Global and _global are both illegal
names in the global namespace.

Also not mentioned yet, names containing a double underscore anywhere
in the name are reserved for the implementation for any use.
Nevertheless, to keep things simple it is probably a good
idea for a program not to use a name with a leading underscore anywhere -
even when it may be OK to do so.

Agreed. And the same for names containing a double underscore. I know
what the rules are and I know where to look them up to remind myself
if I forget (mainly because this question arises here every so often).
But I see no advantage in demonstrating in code how clever I am by
allowing myself to use leading underscores where they are legal. Far
simpler to just avoid leading underscores and double underscores
completely (and I don't find that restriction causes me any difficulty
at all).

Gavin Deane
 
M

Marcus Kwok

Tim H said:
I know C's rules about reserved identifiers (anything starting with
"_", leading "is", etc).

I don't know C++ rules. I assume the same rules and more apply?

I found this (no longer maintained) page:
http://web.archive.org/web/20040416031024/oakroadsystems.com/tech/cppredef.htm
I've seen several conventions for member variables:

foo_
m_foo
_foo

In the case of member variables are _ prefixes still reserved?

Others have answered this. My personal convention is to use the
trailing underscore.
 
R

red floyd

Erik said:
Any name beginning with a double underscore or an underscore followed
by an uppercase letter is reserved for any use, in short, don't use.
Any name beginning with an underscore is reserved for use in the
global namespace, meaning be careful.

Also according to 17.3.4.1.2/2 footnote 165, leading underscore
identifiers are reserved in the std:: namespace as well.
 
R

red floyd

red said:
Also according to 17.3.4.1.2/2 footnote 165, leading underscore
identifiers are reserved in the std:: namespace as well.

Of course, with the exception of template specializations, the *entire*
std:: namespace is reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top