# and # # in a C + + macro

Source: Internet
Author: User

The function of the # in a macro is to string the macro arguments that follow it (stringizing operator), simply by adding a double quote to the left and right of the macro variable it refers to.

The following two statements are equivalent, as defined #define STRING (x) #x .

char *pchar = "Hello";

Char *pchar = STRING (hello);

And a #@ is a single quote (Charizing Operator)

#define MAKECHAR (x) #@x

char ch = makechar (b); with char ch = ' B '; equivalence.

But there are small issues to note that the macro encounters # or # #时就不会再展开宏中嵌套的宏了. What's the point? For example, using Char *pchar =string (__file__), although __file__ itself is a macro, the compiler does not expand it, so PChar will point to "__file__" instead of the source file name of the form "D:\XXX.cpp" you want. Therefore, to add an intermediate conversion macro, first parse the __file__ into a "D:\XXX.cpp" string.

Define two macros as shown below:

#define _STRING (x) #x

#define STRING (x) _string (x)

Call the following statement again to output the source file path with ""

char* PChar = STRING (__file__);

printf ("%s%s\n", PChar, __file__);

You can compare the difference between the next string (__file__) and the __file__ , with double quotes before it, and no double quotes in the latter one.

Again, the next # #的功能, it can be spliced with symbols (token-pasting operator).

There is an example on MSDN:

#define PASTER (N) printf ("token" #n "=%d\n", token# #n)

int token9 = 100;

Call Paster (9) Again, and token# #n直接合并变成了token9 after the macro expands. The entire statement becomes a

printf ("token" "9" "=%d", token9);

In the C language, two concatenated double quotes in the string are automatically ignored, and the above sentence is equivalent to

printf ("Token9 =%d", token9);.

i.e. Output TOKEN9 = 100

With the basics above, we'll look at example 1.

#define WIDEN2 (x) L # # X

#define Widen (x) WIDEN2 (x)

#define __WFILE__ Widen (__file__)

wchar_t *pwsz = __wfile__;

The L in the first macro is the conversion of an ANSI string into a Unicode string. such as: wchar_t *pstr = L "Hello";

Then see wchar_t *pwsz = __wfile__;

__WFILE__ is first expanded into widen (__file__), then expanded into WIDEN2 ("__file__-represented string"), then stitched into L "__file__-represented string" i.e. L "D:\XXX.cpp" The Unicode string is obtained and the string address is assigned to the PWSZ pointer.

In VC _t (), TEXT () is also used in this technique.

In the Tchar.h header file, you can find:

#define _T (x) __t (x)

#define __T (x) L # # X

In the WinNT.h header file, you can find

#define TEXT (quote) __text (quote)//R_winnt

#define __text (quote) l# #quote//R_winnt

So it's easy to understand why the third statement will go wrong error C2065: ' Lsztext ': undeclared identifier

wprintf (Text ("%s%s\n"), _t ("Hello"), text ("Hello"));

Char sztext[] = "Hello";

wprintf (Text ("%s%s\n"), _t (Sztext), text (Sztext));

When you define "Hello" as a macro, you can run it correctly.

#define SZTEXT "Hello"

wprintf (Text ("%s%s\n"), _t (Sztext), text (Sztext));

# and # # in a C + + macro

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.