Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

macros

I was looking through the DXUTCore project that comes with the DirectX March 2009 SDK, and noticed that instead of making normal accessor methods, they used macros to create the generic accessors, similar to the following:

#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 just inserts the text from the second argument into the macro to create a function operating on a variable using that text. Is this something that is standard in C++ (i.e. not Microsoft specific)? Is its use considered good practice? And, what is that operator called?

like image 930
bsruth Avatar asked Jul 13 '09 20:07

bsruth


People also ask

What is the purpose of the word the?

The word the is considered a definite article because it defines the meaning of a noun as one particular thing. It's an article that gives a noun a definite meaning: a definite article. Generally, definite articles are used to identify nouns that the audience already knows about.

Is it the purpose of or the purpose for?

The "purpose of" a shoe is protecting your feet. A possible "purpose for" a shoe is to smash bugs. So "purpose of" describes a property or capacity of a shoe, where "purpose for" describes what might be done with a shoe. Save this answer.

What is the real purpose of life?

Your life purpose is your contribution However, true purpose is about recognizing your own gifts and using them to contribute to the world—whether those gifts are playing beautiful music for others to enjoy, helping friends solve problems, or simply bringing more joy into the lives of those around you.

What is purpose and examples?

Purpose DefinitionThe object for which something exists or is done; end in view. Webster's New World. Similar definitions. The definition of a purpose is a goal or intention. An example of purpose is people meeting to discuss how to cut costs within a company.


2 Answers

Token-pasting operator, used by the pre-processor to join two tokens into a single token.

like image 88
Michael Avatar answered Nov 09 '22 20:11

Michael


This is also standard C++, contrary to what Raldolpho stated.

Here is the relevant information:

16.3.3 The ## operator [cpp.concat]

1 A ## preprocessing token shall not occur at the beginning or at the end of a replacement list for either form of macro definition.

2 If, in the replacement list, 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. If the result is not a valid preprocessing token, the behavior is undefined. The resulting token is available for further macro replacement. The order of evaluation of ## operators is unspecified.

like image 34
GManNickG Avatar answered Nov 09 '22 21:11

GManNickG