 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Louis Lavery Guest
|
Posted: Fri Oct 08, 2004 12:55 am Post subject: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
"Always use ONLY_UPPERCASE_NAMES for macros", say Herb
Sutter and Andrei Alexandrescu in October's CUJ.
They don't say why?
The only macros I use, besides assert (err..), are
header guards and I do them in lower case. Why?
Because I use camel notation, am in the habit of
writing files thus...
/* someFileName.hpp*/
#ifndef someFileName_hpp // <- cut and paste root from above
#define someFileName_hpp // <- cut and paste root from above
#endif
/* someFileName.hpp end */
/* someFileName.cpp */
#include "someFileName.hpp" // <- cut and paste root from above
/* someFileName.cpp end */
....and am lazy, so just cut and paste the root filename
as and when.
So, should I give up this heresy?
More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
Louis.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Fri Oct 08, 2004 11:16 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Louis Lavery wrote:
| Quote: | More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
|
The header guards you mentiened are one case where this is not important.
However, assume the evil '#define square(x) ((x)*(x))'. If that was
uppercased, I'd be aware that evaluating the parameter must not have
side-effects because it is a macro. A normal, lowercase(or camelcase)
spelling does not have the same effect. I'd rather prefer having a rule
that says that macros must always be written in bright, flashing red, as a
warning that this is not a properly behaving function.
Now, why exactly uppercase? That is just a common way to do it, and it also
strikes out from normal text. If we were used to something else, it might
be different, so this is rather a human than a technical requirement.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Fri Oct 08, 2004 2:58 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
"Louis Lavery" <louis (AT) laver (DOT) demon.co.uk> wrote
[...]
| Quote: | More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
|
The problem relates to how the preprocessor is unaware of scopes. The
only way to ensure that there will never be unexpected interferences
between macros and language level identifiers is to make sure that the
names you use come from disjoint sets. In fact the above advice is
incomplete:
Always use ONLY_UPPERCASE_NAMES for macros and use it only for macros.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri Oct 08, 2004 4:12 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Hi,
Louis Lavery wrote:
| Quote: | More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
|
Louis Lavery already pointed out most important things, but there may be
one more: considering the fact that uppercase macro is a common way to
do it, there may be scripting-like tools which use this convention when
searching through source files. C++ is already terrible for parsing and
simple tools tend to use whatever hint they can find. Uppercase is a
common indicator that the word is a macro, not only to humans.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ 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
|
Posted: Fri Oct 08, 2004 4:13 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Louis Lavery wrote:
| Quote: | "Always use ONLY_UPPERCASE_NAMES for macros", say Herb
Sutter and Andrei Alexandrescu in October's CUJ.
They don't say why?
snip |
A programmer working on a large system generally won't know every
single macro defined by every single header that is included in a
source file, directly or indirectly, so he can't be sure that his
chosen identifiers aren't actually macro names (and won't later be
used as macro names) unless macro names all belong to some easily-
remembered subset of the possible names. This subset is
conventionally defined to be the union of the set of names with no
lower-case letters and the set of macro names used by the standard
library. (Thankfully in the C++ standard library there are only 11
macros containing lower-case letters so they are not too hard to
remember.)
--
Ben Hutchings
Never attribute to conspiracy what can adequately be explained by stupidity.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Sat Oct 09, 2004 10:39 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Louis Lavery wrote:
| Quote: | "Always use ONLY_UPPERCASE_NAMES for macros", say Herb
Sutter and Andrei Alexandrescu in October's CUJ.
They don't say why?
The only macros I use, besides assert (err..), are
header guards and I do them in lower case. Why?
Because I use camel notation, am in the habit of
writing files thus...
/* someFileName.hpp*/
#ifndef someFileName_hpp // <- cut and paste root from above
#define someFileName_hpp // <- cut and paste root from above
#endif
/* someFileName.hpp end */
/* someFileName.cpp */
#include "someFileName.hpp" // <- cut and paste root from above
/* someFileName.cpp end */
...and am lazy, so just cut and paste the root filename
as and when.
So, should I give up this heresy?
More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
|
Header guards are (IMHO) more interesting beasts and they only HAPPEN_TO_BE
macros, but they are rather a hack than a solution. I do not know if there
are any plans to include something like pragme once from MS into the
standard, but it would be beneficial.
Supposed that you have two headers using the ExcaltySame filename. And
their guards happen to be the same macro! It does not matter that you have
included them as
#include "foo/magic.h"
#include "bah/magic.h"
the second header will never get included... Only the compiler is in a
position to make up a unique ID for each header in a compilation unit.
That said I do not think that header guards should be treated as macros.
Especially that on Unices FAT.h and fat.h and Fat.h and FaT.h etc. are ALL
DiFeReNt files. So if the guards are all converted to uppercase what you
get is more chance of failure.
--
WW aka Attila
:::
Honesty is always the best policy... As long as you're not dealing with
women.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Louis Lavery Guest
|
Posted: Sun Oct 10, 2004 9:22 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Nicola Musatti <Nicola.Musatti (AT) ObjectWay (DOT) it> wrote
| Quote: | "Louis Lavery" <louis (AT) laver (DOT) demon.co.uk> wrote
[...]
More to the point, what is the rationale behind the advice
"Always use ONLY_UPPERCASE_NAMES for macros"?
The problem relates to how the preprocessor is unaware of scopes. The
only way to ensure that there will never be unexpected interferences
between macros and language level identifiers is to make sure that the
names you use come from disjoint sets. In fact the above advice is
incomplete:
Always use ONLY_UPPERCASE_NAMES for macros and use it only for macros.
|
Yes, that makes sense, you put it over very well, thanks.
I think I'll stay lazy as I now prepend namespaces, my shorter
include guards look something like...
#ifndef devilsChimney_geom2d_vector_hpp
Thanks again,
Louis.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Sun Oct 10, 2004 9:39 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
[email]wolof (AT) freemail (DOT) hu[/email] (White Wolf) wrote (abridged):
| Quote: | Supposed that you have two headers using the ExcaltySame filename. And
their guards happen to be the same macro! It does not matter that you
have included them as
#include "foo/magic.h"
#include "bah/magic.h"
|
You should avoid giving different headers the same header guard. Generally
header guards, namespaces and directory structure should be in accord. For
example, the first header might look like:
#ifndef foo_magic_h_
#define foo_magic_h_
namespace foo {
// ...
}
#endif
The "foo/" prefix on the header name, the "foo_" prefix in the header
guard and the "foo" namespace are all doing the same thing - organising
and managing names. Whatever reason prompted you to use the "foo/" prefix
should apply to the other two.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Sun Oct 10, 2004 11:06 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Dave Harris wrote:
| Quote: | wolof (AT) freemail (DOT) hu (White Wolf) wrote (abridged):
Supposed that you have two headers using the ExcaltySame filename. And
their guards happen to be the same macro! It does not matter that you
have included them as
#include "foo/magic.h"
#include "bah/magic.h"
You should avoid giving different headers the same header guard.
Generally header guards, namespaces and directory structure should be in
accord. For example, the first header might look like:
|
I should. Yes. However foo is a library, coming from a 3rd party, and it
is installed on a read-only area of the corporate network. Same for bar.
So I still think, that using macros for header guards is a hack, and so it
should be replaced with something which is guaranteed to work.
--
WW aka Attila
:::
For attractive lips, speak words of kindness - Audrey Hepburn
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon Oct 11, 2004 11:15 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
[email]wolof (AT) freemail (DOT) hu[/email] (White Wolf) wrote (abridged):
| Quote: | You should avoid giving different headers the same header guard.
Generally header guards, namespaces and directory structure should be
in accord.
I should. Yes. However foo is a library, coming from a 3rd party, and
it is installed on a read-only area of the corporate network. Same for
bar.
|
That doesn't absolve you of your responsibility to manage your namespaces.
You have two 3rd party libraries both trying to define the concept
"magic". The conflict is deeper than the header guards.
You might consider writing a wrapper header which includes the third party
header with suitable decorations. (Probably involving #undef foo_hpp_.)
| Quote: | So I still think, that using macros for header guards is a hack, and so
it should be replaced with something which is guaranteed to work.
|
I don't really object. Obviously #pragma is not guaranteed to work, so it
needs a language change. As I recall, there are some technical problems
with getting the compiler to decide when two files are the same. You have
to consider Unix file system links, for example. Arguably the whole
textual #include scheme should be replaced by something a bit more modular
anyway. A good solution is surely 5 or 10 years away, if one ever comes.
Meanwhile header guards are the best portable solution available.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John M. Dlugosz Guest
|
Posted: Tue Oct 12, 2004 10:35 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
"White Wolf" <wolof (AT) freemail (DOT) hu> wrote in message >
| Quote: |
Supposed that you have two headers using the ExcaltySame filename. And
their guards happen to be the same macro! It does not matter that you have
included them as
|
That's why I use a generated UUID, with dashes changed to underbars
and a leading "I". (my guidelines noted at
<http://www.dlugosz.com/CPP>)
Actually, I type "#pragma once", and change it to the #if wrapper
using a trivial Perl script if (and only if) I move the code to a
compiler that doesn't have a more direct (but non-standard) mechanism
like #pragma once.
--John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francisco Almeida Guest
|
Posted: Tue Oct 12, 2004 10:01 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
[email]11lrhap02 (AT) sneakemail (DOT) com[/email] (John M. Dlugosz) wrote in message news:<78557888.0410111844.3e0bb1ea (AT) posting (DOT) google.com>...
| Quote: | "White Wolf" <wolof (AT) freemail (DOT) hu> wrote in message
Supposed that you have two headers using the ExcaltySame filename. And
their guards happen to be the same macro! It does not matter that you have
included them as
That's why I use a generated UUID, with dashes changed to underbars
and a leading "I". (my guidelines noted at
http://www.dlugosz.com/CPP>)
Actually, I type "#pragma once", and change it to the #if wrapper
using a trivial Perl script if (and only if) I move the code to a
compiler that doesn't have a more direct (but non-standard) mechanism
like #pragma once.
--John
|
Why hasn't the C++ language included a standard #unique preprocessor
(or something similar) yet, if these #ifndef/#endif's are becoming so
annoying?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Wed Oct 13, 2004 11:31 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Dave Harris wrote:
| Quote: | wolof (AT) freemail (DOT) hu (White Wolf) wrote (abridged):
You should avoid giving different headers the same header guard.
Generally header guards, namespaces and directory structure should
be in accord.
I should. Yes. However foo is a library, coming from a 3rd party,
and it is installed on a read-only area of the corporate network.
Same for bar.
That doesn't absolve you of your responsibility to manage your
namespaces. You have two 3rd party libraries both trying to define
the concept "magic". The conflict is deeper than the header guards.
|
That is solved by namespaces.
| Quote: | You might consider writing a wrapper header which includes the third
party header with suitable decorations. (Probably involving #undef
foo_hpp_.)
|
You might, I do not have the resources for it.
| Quote: | So I still think, that using macros for header guards is a hack, and
so it should be replaced with something which is guaranteed to work.
I don't really object. Obviously #pragma is not guaranteed to work,
so it needs a language change. As I recall, there are some technical
problems with getting the compiler to decide when two files are the
same. You have to consider Unix file system links, for example.
|
AFAIK it is possible on Unix/Posix to ask for the inode of the file and then
know it is the same That is for hard link. For softlinks it is possible to
ask for the "pointed" filename.
| Quote: | Arguably the whole textual #include scheme should be replaced by
something a bit more modular anyway. A good solution is surely 5 or
10 years away, if one ever comes.
|
Well, I am afraid until it comes into C it will not come into C++. :-(
| Quote: | Meanwhile header guards are the best portable solution available.
|
Not the header guards I have seen. They are portable on Windows (all
uppercase). On Unix the header ME.hpp and me.hpp are not the same.
Attila aka WW
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Wed Oct 13, 2004 11:33 am Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
Francisco Almeida wrote:
| Quote: | Why hasn't the C++ language included a standard #unique preprocessor
(or something similar) yet, if these #ifndef/#endif's are becoming so
annoying?
|
Because they're not annoying, and because the proposed directives
have difficulty in defining file identity well enough to specify
what they do.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Thu Oct 14, 2004 8:33 pm Post subject: Re: Always use ONLY_UPPERCASE_NAMES for macros |
|
|
[email]attila.feher (AT) lmf (DOT) ericsson.se[/email] (Attila Feher) wrote (abridged):
| Quote: | That doesn't absolve you of your responsibility to manage your
namespaces. You have two 3rd party libraries both trying to define
the concept "magic". The conflict is deeper than the header guards.
That is solved by namespaces.
|
I suppose my question is, why use namespaces in the code and not in the
header guard?
I suspect that the 3rd party code that gets the header guard wrong will
also get the namespace wrong, and you'll have to edit the code to fix the
one so you can fix the other at the same time.
| Quote: | On Unix the header ME.hpp and me.hpp are not the same.
|
Header guards should be case sensitive. Which brings us back to the root
article. Header guards are an example of macros which should /not/ be all
in capitals.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|