Re: Exhaustive list of unusable names
"Frederick Gotham" <fgothamNO@SPAM.com> wrote in message
news:Xns97E8BFFB8B436fgothamNOSPAM@194.125.133.14...
The dangers and inadequacies of macros are well known.
As are their uses.
Viable substitutes like "typedef", "enum", templates and global const
variables are preferable.
When possible, yes.
Namespaces also give us more freedom as to what names we can use
ourselves.
Except for library defined macros, yes. So don't try to reuse setjmp
or errno 'cause they're still macros.
The C++ Standard explicitly lists all of its keywords. However, I think
it would be helpful if it also gave a list of all the macros it
defines... thus providing an exhaustive list of "names" which we simply
can't use in C++.
See the index page of the Dinkum Compleat Libraries Reference
at our web site. It shows *all* the names defined in Standard C,
Standard C++, and the four TRs (including TR1). Prudence dictates
that you avoid all these.
For instance, the following program won't compile for me:
#include <climits>
void Func( int CHAR_BIT ) {}
int main(){}
And it is *extremely* foolish of you to use a name written in all
caps that begins with CHAR_, knowing the propensity of the C and
C++ Standards to define such names. (If nothing else, you mislead
future readers.) It is also moderately foolish to use all caps for
anything but a macro name.
I have another example, reproduced below. If memory serves me right, I
did at one time have a compiler which wouldn't compile it. Do you think
it should compile? It's based on creating our own function called
"strlen".
#include <cstddef>
#include <cstring>
std::size_t strlen( const char* p )
{
std::size_t len = -1; /* Max value */
while ( ++len, *p++ );
return len;
}
#include <iostream>
using std::cout;
#include <cstdlib>
int main()
{
cout << strlen("Hello") << '\n';
cout << std::strlen("Hello") << '\n';
system("PAUSE");
}
We're in trouble with the above code if the STL version of strlen is a
macro.
Well, that's not supposed to happen, because the C++ Standard disallows
the use of masking macros for library function names. Nevertheless,
many implementations still supply those macros. Even absent any masking
macro, many implementations define strlen in the global namespace even
when you include a <c*> header. (That's so commonplace it'll likely
be blessed as conforming in the next revision of the C++ Standard.) But
on top of all that, you still don't know the linkage of the strlen defined
by the Standard C++ library. So your program is trebly foolish to push its
luck in this area.
So... to summarise, I think it would be helpful to have an exhaustive
list of all the macros which a compliant C++ compiler may define (except
of course for the ones with leading underscores).
Why omit those? _IOFBF, _IOLBF, and _IONBF are part of the external
interface to both the Standard C and C++ libraries.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.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://www.comeaucomputing.com/csc/faq.html ]