C++11 中的空宏参数合法吗?

Are empty macro arguments legal in C++11?(C++11 中的空宏参数合法吗?)

本文介绍了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)

技术上仍有 3 个参数;只是第一个是空的".这个问题与可变参数宏无关.

There are still technically 3 arguments; it's just that the first one is "empty". This question is not about variadic macros.

当我这样做时,我在使用 -ansi(又名 -std=c++98)进行编译时收到来自 g++ 的警告,但在我使用 时却没有-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/10提到:

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.

我认为空参数属于表示由以下组成的参数上面没有预处理标记.
此外,国际标准编程语言 C rev 的基本原理中的 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 的一个新特性:类似函数的宏调用现在也可以有空参数,也就是说,一个参数可以由 no预处理令牌.

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 中的空宏参数合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++11 中的空宏参数合法吗?

基础教程推荐