Use double colons (::) in #define

后端 未结 1 1010
时光说笑
时光说笑 2021-01-17 09:29

May I use double colons in #define ? I\'d like to save some writing in the implementation files, e.g. like this:

// foo.h
#define template templ         


        
相关标签:
1条回答
  • 2021-01-17 10:29

    No. The name of a macro must be an identifier; it can't consist of other characters and it can't consist of multiple tokens.

    #define template is invalid because template is not an identifier, it is a keyword.

    #define foo:: foo<T>:: was valid in C90 and C++98: it defines a macro named foo that is replaced by :: foo<T>:: (that's not what you want to do, but it was valid). However, this is invalid in C99 and C++11 because in the newer revisions of the languages, there must be whitespace between the name of an object-like macro and its replacement list (the tokens with which it is replaced).

    0 讨论(0)
提交回复
热议问题