Can't understand the widen __FILE__ macro

Started by
0 comments, last by Mike nl 15 years, 10 months ago
Here is the code supplied by microsoft as to how to widen the __FILE__

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
Why is there a need to have 2 widen functions? I tried this:

#define WIDEN(x) L ## x
#define __WFILE__ WIDEN(__FILE__)
and it doesn't compile. Is there some rules as to how the compiler interpret macros and expands them??
Advertisement
In your case, it expands WIDEN(__FILE__) to L ## __FILE__, which prevents __FILE__ from being expanded before the concatenation (see ISO C 6.10.3.1). So it becomes L__FILE__, which doesn't exist.

In the other case, WIDEN(__FILE__) expands to WIDEN2("blah.cpp"), because __FILE__ is not directly preceded by ## and WIDEN2 is only expanded after all parameters to WIDEN have been expanded. With WIDEN2 it becomes L ## "blah.cpp" => L"blah.cpp"

So in short, yes, there are rules to this :)

Quote:
2. If, in the replacement list of a function-like macro, a parameter is immediately preceded or followed by a ## preprocessing token, the parameter is replaced by the corresponding argument's preprocessing token sequence;

3. For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token

(emphasis mine)
Million-to-one chances occur nine times out of ten!

This topic is closed to new replies.

Advertisement