Jump to content
  • Advertisement
Sign in to follow this  
littlekid

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!