Reputation: 43
Can someone explain why the following doesn't work?
int main() // Tried on several recent C++ '03 compilers.
{
#define FOO L
const wchar_t* const foo = FOO"bar"; // Will error out with something like: "identifier 'L' is undefined."
#undef FOO
}
I thought that preprocessing was done in an earlier translation phase than string literal operations and general token translation.
Wouldn't the compiler be more or less seeing this:
int main()
{
const wchar_t* const foo = L"bar";
}
It would be great if someone could cite an explanation from the standard.
Upvotes: 4
Views: 409
Reputation: 455272
Use:
#define FOO L\
without the trailing \
there will be a space between L
and the string on macro substitution. This is from the result of g++ -E
:
const wchar_t* const foo = L "bar";
Upvotes: 6
Reputation: 15126
As an alternative to John's answer, I think you could define this the way Microsoft's _T() is defined:
#define FOO(x) L ## x
and use it like this:
FOO("bar")
This will concatenate the L with the text appropriately.
Upvotes: 1
Reputation: 2704
Look at the output from the preprocessor (g++ -E)
# 1 "ttt.cc"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "ttt.cc"
int main()
{
const wchar_t *const foo = L "FOO";
}
There's a space after your L.
Upvotes: 0
Reputation: 48287
The error message you're getting means that it is preprocessing your #define
before it does anything else, it just doesn't know what it ends up meaning (after replacing all FOO
s with L
s, it looks for what L
means and can't figure it out).
The compiler is seeing your second bit of code, it just doesn't know what L"bar"
means at that point.
Are you sure everything is defined and included properly?
Upvotes: 0