[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Macros with G++
From: |
Guy Harrison |
Subject: |
Re: Macros with G++ |
Date: |
Sun, 26 Sep 2004 01:00:54 GMT |
User-agent: |
KNode/0.7.7 |
Michael Meier wrote:
> Hi,
>
> I'm not sure weather this is a compiler error or sonething different. The
> code:
>
> #include <stdio.h>
> #include <wchar.h>
>
> //#define WIDE(x) L#x doesnt work
"Stringify"...
L#x = "x"
> #define WIDE(x) L##x
Token Pasting...
L##x = Lx
> int main( void )
> {
> //wchar_t test = WIDE( Hello World ); doesnt work
> wchar_t* test = WIDE( "Hello World" );
> return 4;
> }
>
> The question is why the first approach doesn't work. g++ complains that L
> has not been defined. One hash should expand the variable and put it into
> quotes whereas two hashes just expand the variable. It works with two but
> not with one hash. Any explanation is welcome!
Your second version is the correct approach. Something like this...
#if defined(UNICODE)
# define APP_L(x) L##x
# define APP_C wchar_t
#else
# define APP_L(x) x
# define APP_C char
#endif
const APP_C s[] =APP_L("ook");
const APP_C c =APP_L('a');
It's easier in C++ ...
<some app global header>
typedef std::basic_ostream< APP_C > AppOs;
typedef enum {
APP_COUT,APP_CERR,APP_CLOG
} APP_LOG;
AppOS & AppLog(APP_LOG =APP_CERR);
</some app global header>
<some compilation unit>
#if defined(UNICODE)
# define APP_COUT std::wcout
...
#else
# define APP_COUT std::cout
...
#endif
AppOS &
AppLog(APP_LOG x)
{
switch (x)
case APP_COUT: return APP_COUT;
...
}
}
</some compilation unit>
#include "some global header"
int main()
{
AppLog() << APP_L("at last!\n");
return 0;
}
You'll need a unicode capable terminal. In the case of windows, this is one
area where it's much easier (eg: filenames etc). Doing it transparently in
C results in macro hell, re: windows again - winapi headers!