What is the purpose of the ## operator in C ++ and what is it called?

I looked at the DXUTCore project that ships with the DirectX March 2009 SDK and noticed that instead of using the usual access methods, they used macros to create shared accessories, as shown below:

#define GET_ACCESSOR( x, y )  inline x Get##y()  { DXUTLock l; return m_state.m_##y;};
...

GET_ACCESSOR( WCHAR*, WindowTitle );

It seems that the ## operator simply inserts the text from the second argument into the macro to create a function that acts on the variable using this text. Is this standard for C ++ (i.e. not for Microsoft)? Is its use used by good practice? And what is called this operator?

+5
source share
7 answers

, .

+16

++, Raldolpho.

:

16.3.3 ## [cpp.concat]

1 A ## .

2 , ## , .

3 - , , ## ( ) . , undefined. . ## .

+6

, ( ). , Microsoft.

+5

++, C. .

? , , , ( ) Goto.

: -, , " ++, C". . , , ++ C.

+3

, , :

#define MyMacro(A,B) A ## B
MyMacro(XYZ, 123) // Equivalent to XYZ123

, MISRA C , ( # 'stringify') - .

+2

This is the point insertion operator allowed by standard C ++ (for more details see 16.3.3). Regarding good practice: using a macro is not a good IMHO practice (in C ++).

+2
source

this is concatenation for ie macro arguments

GET_ACCESSOR (int, Age);

will be spent on

inline int GetAge()  { DXUTLock l; return m_state.m_Age;};
+1
source

All Articles