C ++ 11中的空宏参数是否合法? [英] Are empty macro arguments legal in C++11?
问题描述
我有时故意省略宏参数。例如,对于像
I sometimes deliberately omit macro arguments. For example, for a function-like macro like
#define MY_MACRO(A, B, C) ...
我可以称为:
MY_MACRO(, bar, baz)
它只是第一个是空。
当我这样做时,当使用 -ansi
编译时,会从g ++收到警告, (aka -std = c ++ 98
),但是当我使用 -std = c ++ 0x
这是否意味着空的宏参数在新的C ++标准中是合法的?
When I do this I get warnings from g++ when compiling with -ansi
(aka -std=c++98
), but not when I use -std=c++0x
. Does this mean that empty macro args are legal in the new C++ standard?
这是我的问题的全部,但预计你为什么要?响应,这里是一个例子。我喜欢保持.h文件由函数体整理,但在.h文件之外实现简单的访问器是乏味的。因此,我写了以下宏:
That's the entirety of my question, but anticipating the "why would you want to?" response, here's an example. I like keeping .h files uncluttered by function bodies, but implementing simple accessors outside of the .h file is tedious. I therefore wrote the following macro:
#define IMPLEMENT_ACCESSORS(TEMPLATE_DECL, RETURN_TYPE, CLASS, FUNCTION, MEMBER) \
TEMPLATE_DECL \
inline RETURN_TYPE* CLASS::Mutable##FUNCTION() { \
return &MEMBER; \
} \
\
TEMPLATE_DECL \
inline const RETURN_TYPE& CLASS::FUNCTION() const { \
return MEMBER; \
}
这是我如何将其用于类模板, int
int _
:
This is how I would use it for a class template that contains an int
called int_
:
IMPLEMENT_ACCESSORS(template<typename T>, int, MyTemplate<T>, Int, int_)
对于非模板类,我不需要 template< typename T>
,所以省略宏参数:
For a non-template class, I don't need template<typename T>
, so I omit that macro argument:
IMPLEMENT_ACCESORS(, int, MyClass, Int, int_)
推荐答案
如果我理解正确,从C99和
C ++ 0x(11)允许空宏参数。
C99 6.10.3 / 4说:
If I understand correctly, empty macro argument is allowed since C99 and
C++0x(11).
C99 6.10.3/4 says:
...参数数量(包括由
组成的参数,无需预处理令牌)参数个数...
... the number of arguments (including those arguments consisting of no preprocessing tokens) shall equal the number of parameters ...
和C ++ N3290 16.3 / 4有相同的语句,而C ++ 03 16.3 /
and C++ N3290 16.3/4 has the same statement, while C++03 16.3/10 mentions:
...任何参数都不包含预处理标记,行为是
未定义。
... any argument consists of no preprocessing tokens, the behavior is undefined.
我认为空参数是由上面的
无预处理令牌组成的表示参数。
此外,6.10.3在国际标准编程语言的基础中。 5.10
说:
I think empty argument comes under the representation arguments consisting of
no preprocessing tokens above.
Also, 6.10.3 in Rationale for International Standard Programming Languages C rev. 5.10
says:
C99的一个新功能:函数式宏调用现在也可能是
有空参数,也就是说,一个参数可能没有
预处理令牌。
A new feature of C99: Function-like macro invocations may also now have empty arguments, that is, an argument may consist of no preprocessing tokens.
这篇关于C ++ 11中的空宏参数是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!