lallous said:
Thank you all for your replies.
For those who replied:
That's what I suspected w/o trying the code, but after testing it, it didn't
work.
I had to specify which namespace that #define belongs to.
The code I ran into isn't mine and seeing a #define inside a namespace made
me pose this question to the newsgroup.
For those who have VC7, please try this code:
#include <string>
namespace std
{
#ifdef UNICODE
//typedef string stringT;
#define stringT wstring
#else
//typedef wstring stringT;
#define stringT string
#endif
}
int main()
{
// works:
std::stringT test;
// doesn't work (unless you state the namespace) (error C2065)
stringT test1;
return 0;
}
Of course it doesn't work. This is completely standard behaviour. You are
misunderstanding what is happening. It is because the macro is working that
you get an error, not because it isn't. Macros pay no attention to
namespaces at all.
In the code that doesn't work the macro stringT expands to string, so the
line of code looks like this
string test1;
In the code that does work the macro stringT expands to string, so the line
of code looks like this
std::string test;
The case that doesn't work is an error because string is not qualified with
a namespace, macros have nothing to do with it. In /both/ cases the macro
worked and expanded to exactly the same result.
Undoubtedly typedefs are the correct way to do it, but adding typedefs to
the std namespace is not legal, even if VC accepts it. Here is how you
should do it
#include <string>
#ifdef UNICODE
typedef std::string stringT;
#else
typedef std::wstring stringT;
#endif
or you could even do it like this (VC++ specific code)
#include <string>
#include <tchar.h>
typedef std::basic_string<_TCHAR> stringT;
john