Why _type (underscore before identifier and variable)

Pages: 12
Why do programmers use underscores to name variables and data types? It grinds my gears a bit. I keep seeing abuse of typedef. It makes it suck to code using those applications because typing an underscore is slightly slower, induces pauses, than just coding letters. I understand that it is a convention, but its just not a very clean convention in my opinion.

For example: __stdcall, __stdcall__, __int64, _DEBUG, __DEBUG__, __linux__. Adding underscores makes coding clunkier.

1
2
typedef this __this; //why Microsoft?
typedef Vector3  __Vec3; //whats the point then 


I do it for variables though, only when the parameter would shadow the instance data.

like
1
2
3
4
5
class Point{
int x,y;
public: //explicit
Point( int _x, int _y ):x(_x),y(_y){}
};
Last edited on
All names starting with an underscore or two underscores are reserved by the implementation. So Microsoft can do that you may not.:)
This is being done that to prevent a collision with user defined names.
Last edited on
Point( int _x, int _y ):x(_x),y(_y){}
It's not necessary to use different names in this case.

Point( int x, int y ):x(x),y(y){}
This will do the exact same thing.
@Peter87
Not for my compiler for class methods. Thats why I do underscore for shadowed parameters, just to make sure.
1
2
3
4
5
6
7
8
void set(int x, int y ){
 x = x;
y = y;
/*
this->x = x;
this->y = y;
*/
}
Last edited on
The point of this so you less likely to run into name collision or accidently use function, variable or constant, which could be changed or deleted in next version of libraries.
> All names starting with an underscore or two underscores are reserved by the implementation.

We can have a name with a leading underscore in our code, provided that

a. the name is not in the global namespace
b. the leading underscore is not followed by another underscore or an upper case letter.
From the C++ Standard that says

1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase
letter (2.12) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the
global namespace.


does not follow that the first point is relative to the global namespace though the paragraph in whole is named "Global names".
> the C++ Standard that says...

So the standard makes it abundantly clear that a local names like _x and non-global names like boost::lambda::_1 are not reserved for the implementation.

What else is new?
If to read the following phraze carefully

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


then it follows that each name that begins with an underscore is reserverd irrespectively of where it is declared. Such a name the implementation can use as a name in the global namespace.
Last edited on
then it follows that each name that begins with an underscore is reserverd irrespectively of where it is declared.


It follows that each name that begins with an underscore is reserved for use as a name in the global namespace. It follows that such a name is not reserved for use by the implementation as a name outside of the global namespace.

"Each name that begins with an underscore" is simply describing the names so reserved. It does not say "Each occurrence of a name, regardless of the namespace or scope that it occupies, that begins with an underscore..." or "Each name that begins with an underscore is reserved to the implementation."
Last edited on
@cire

Read carefully what you wrote yourself.:)

The fiirst part: ""Each name that begins with an underscore" is simply describing the names so reserved"
And what does mean "simply rdescribing so eserverd" compared with "not simply describing so reserved"?:)
If a name as you said "reserved" when it means that it is reserved irrespective of where the name is declared.:) At least I undesrstand it the way I described. It is not important for which purposes such names are reserved. The problem that such names used by the implemenaion can conflict with the same names declared by you even in other scopes
Last edited on
If a name as you said "reserved" when it means that it is reserved irrespective of where the name is declared.:)


Vlad, you cannot simply ignore "is reserved for use as a name in the global namespace." Had the meaning you are implying been intended there would be no reason to include "in the global namespace."

When you pair that sentence with the one that comes before it, one wonders why they would need two sentences to say what was said if your conclusion was the meaning intended. They could've gotten away with "Each name that contains a double underscore or starts with an underscore is reserved to the implementation for any use."

But they're not reserved for "any use" according the standard.
The phrase consists from two parts.

The fiirst parts says that "Each name that begins with an underscore is reserved".
Does it point out where these names are declared? It is irrespective of where the names are declared they are reserved by the implementation.

The second part of the phrase says for which purpose these names are reserved.: "for use as a name in the global namespace."

So any name that begins with an underscore is reserved by the implementation. One more: any name. And what will do the implementation with such names? It will use them in the global namespace.
Last edited on
The phrase consists from two parts.


The statement is one statement. It's not even a compound statement. Arbitrarily dividing it in two and saying one part has no bearing on the other is silly.


So any name that begins with an underscore is reserved by the implementation. One more: any name. And what will do the implementation with such names? It will use them in the global namespace.

And if only used as a name in the global namespace, what effect will it have on names in function scope or other namespaces that may be identical? None? You don't say. So, it follows that they are reserved, in fact, only in the global namespace.

I'm finished with this discussion.
Well standard clearly states that all names containg double underscore (__name) or underscore followed by uppercase letter (_Name) are reserved for implementation everywhere, in all namespaces.
In addition to that all names in global namespace beginning with underscore are reserved too (::_name but not nonglobalNamespace::_name)
Last edited on
@cire

The statement is one statement. It's not even a compound statement. Arbitrarily dividing it in two and saying one part has no bearing on the other is silly.


I regret that you are unable to understand that any statement can consist from several parts.
The statement says two things. The first that each name (do you understand what means each name?) that begins with an underscore is reserved. Or will you say that the statement does not say this?:)
Further, what is name in this context? It is an identifier. So trhe phrase says that each identifier that begins with an underscore is reserved.
So you can list all identifiers in your program. For example

a,

b12,

123c, (a bad identifier, the compiler will issue an error)

-d;

So for example your program has these four identifiers (names). The Standard says that name _d is reserved by the implementation The Standard says nothing where you are using this identifier. It is unimportant where you are using it. Otherwise the standard will say that for example that names, declared in the global namespace and begin with an underscore are reserved.
The standard simply says that identifiers that begin with underscore are reserved. And then the prase explains for which purpose the implementation reserves such names. It reserves such names for using in the global namespace.

So the phrase consists from two parts. It says 1) what identifiers are reserved and 2) for which using by the implementation they are reserved.

The first that each name (do you understand what means each name?)

Really? Should I now point out that English is a second language for you and your grasp of it is questionable? Your condescending tone certainly invites it.

Perhaps the statement could be better phrased, but in context and with the knowledge that names in one namespace have no effect on names in nested namespaces it's very clear that what you're suggesting is not the intended meaning.

[Edit: typo]
Last edited on
@cire
Perhaps the statement could be better phrased, but in context and with the knowledge that names in one namespace have no effect on names in nested namespaces it's very clear


It is ypur personal fantasies. The phrase says nothing about where the names are declared. It says that each name that begins with an underscore is reserved. It is the second question where the implementation will use these names and for what purpose. For example it could use these names in std:: namespace or somewhere else. It is unimportant. But the standard says clear enough that each name (where did you see that only names in the global namespace are reserved?!) that begins with an underscore is reserved.

Well, having the phrase

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


please answer a simple question without any your comments to your own answer

What names are reserved by the implementation?
Last edited on
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace

It tells that each name starting with underscore in global namespace are reserved. It implies, that such names in namespaces aside from global are not reserved. So according to that quote ::_name is reserved, but someNamespace::_name is not.
There is second part of that quote (well, actually first):
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.

So names like __name or _Name are reserved in any namespaces, not only in global.
http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
Last edited on
@MiiNiPaa

It tells that each name starting with underscore in global namespace are reserved


You are wrong. Read one more the phrase "Each name that begins with an underscore is reserved "

What you bolded deals with where and how such names will be used by the implementaion. It is a different thing.
So the standard says

1) what names are reserved:

Each name that begins with an underscore is reserved to the implementation

and 2) how and where the implementation wll use such a name(s)

for use as a name in the global namespace



Last edited on
Pages: 12