Re: Is there a convention mandating macros to be all-uppper-case?
- From: Tamas Demjen <tdemjen@xxxxxxxxx>
- Date: Thu, 18 May 2006 14:06:19 -0700
ultranet wrote:
#ifdef UNICODE
typedef std::wstring tstring;
typedef proprietary::WString TString;
#else
typedef std::string tstring;
typedef proprietary::String TString;
#endif
I'd rather not do that, to keep namespaces intact.
Do you think macros don't disrupt symbols in other namespaces? Macros are even worse, because they make the substitution completely blindly, without any consideration to their real meaning. At least with typedef, you can be sure it's not going to disrupt types in other namespaces, member functions in other classes, etc.
Every time I #include <windows.h>, it completely messes up a bunch of names. For example, the first thing I have to do is #undef min, #undef max, #undef abs, otherwise my code usually doesn't compile. windows.h macros also redefine a number of .NET classes and member function. Using macros to rename types and function names is pure evil, because it also renames every symbol, without consideration where they belong to. For example, if I roll my own class:
class C
{
public:
std::string TString() const { return "hello"; }
};
Your macro will completely disrupts my member function, because it's renaming it. If it happens to be an imported class from a DLL, it won't link anymore.
The typedef version doesn't have this problem, because it's context sensitive, and won't replace symbols in other namespaces and classes.
Wrap your typedefs into a namespace to avoid name collision with other TString types. That's the solution to namespace conflicts. Macros just make things worse than typedef, not better.
If you absolutely have to use macros, which is inevitable sometimes, make sure they're in all caps and as unique as possible. For example, ULTRANET_TSTRING is unlikely to conflict with anything. A good example to proper macro naming in modern C++ code is BOOST_STATIC_ASSERT.
Tom
.
- Follow-Ups:
- Re: Is there a convention mandating macros to be all-uppper-case?
- From: Simon Trew
- Re: Is there a convention mandating macros to be all-uppper-case?
- References:
- Re: Is there a convention mandating macros to be all-uppper-case?
- From: Ulrich Eckhardt
- Re: Is there a convention mandating macros to be all-uppper-case?
- From: Ulrich Eckhardt
- Re: Is there a convention mandating macros to be all-uppper-case?
- Prev by Date: Re: deprecated copy
- Next by Date: Link error
- Previous by thread: Re: Is there a convention mandating macros to be all-uppper-case?
- Next by thread: Re: Is there a convention mandating macros to be all-uppper-case?
- Index(es):
Relevant Pages
|