Public Group

# Can't understand the widen __FILE__ macro

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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??

##### Share on other sites
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)

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633421
• Total Posts
3011797
• ### Who's Online (See full list)

There are no registered users currently online

×