Little known, undocumented VC++ "macro" called __LPREFIX
Saturday, August 23, 2008, 01:34 PM -
Programming
__LPREFIX is actually not a pre-processor macro though it is used like a macro. It is the MS VC++ compiler and not the pre-processpor which actually handles this. This can be used to add the L-prefix to a string thereby making it a wide string (wchar_t).
Example: The following two lines are the same.
wchar_t* msg = L"Hello World!";
wchar_t* msg = __LPREFIX("Hello World!");
This however does not explain the need to use this prefix. Where would it be useful?
If you have something like
#define MSG "Hello World!"
You cant do
wchar_t* msg = L MSG; //error
The only way to get around this is either to call the Narrow to Wide conversion routines or just say
wchar_t* msg = __LPREFIX( MSG );
If you are using only wchar_t in your code and you want the wide version of macros like __DATE__, __LINE__, __FUNCTION__, etc then this could be helpful.
#define __WDATE__ __LPREFIX( __DATE__ )
#define __WTIME__ __LPREFIX( __TIME__ )
#define __WFUNCTION__ __LPREFIX( __FUNCTION__ )
As a side note, a cross platform (and the correct) way to get the wide version of a predefined macro like __FILE__ is :
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
Another undocumented VC++ related string "macro" is __SPREFIX which is used to create a managed string object(.Net) from a string.
Example:
System::String^ date = __SPREFIX( __DATE__ )
Fun: The MS VC++ 2005, VC++ 2008 compiler crashes if you try to compile the following program:
#include <windows.h>
#define DATA __SPREFIX( __DATE__ )
int main() {
DATA;
return 0;
}
Well, they never documented it anyways.
Related link: Go get
Microsoft Visual Studio 2008 Express Edition. Its free.
Happy Prefixing.//