宏函数有几行参数? [英] Macro function with several lines for the parameter?

查看:134
本文介绍了宏函数有几行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,
我需要定义一个宏。
该宏将作为代码的块作为参数。

In C++, I need to defined a macro. That macro would take as parameter a "block" of code.

我们可以安全地使用几行代码作为宏函数的参数吗?

我问自己如果:


  1. 代码有效,定义为有效的标准,如跨平台?

  2. 是有一个更好的方法来做同样(我不能使用模板功能,上下文)。







#define MY_MACRO( expr ) DOSOMETHING( (expr) ); DOANOTHERTHING( (expr) ); // etc...

int my_function() {
    int o = RandomNumber();
    MY_MACRO( 
        int k = AFunction();
        k++;
        AnotherFunction( k + o ); // here I need to keep the context of the call
    ); 
}

我们不能使用函子,因为我们需要访问上下文调用。
我们不能使用lambda(snif),因为我们使用的旧编译器不提供它(我们不能改变它)。

We can't use functors because we need to have access to the context of the call. We can't use lambda (snif) because we use an old compiler that don't provide it (and we can't change it).

推荐答案

16.3 / 9:


在预处理序列中,
令牌构成一个调用的
函数式宏,新行是
被认为是正常的空格
字符。

Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.


$ b b

所以多行宏调用一般都很好。当然,如果 DOSOMETHING DOANOTHERTHING 不要在范围中引入大括号,那么您的特定示例将重新定义 k

So the multi-line macro invocation in general is fine. Of course if DOSOMETHING and DOANOTHERTHING don't introduce braces for scope, then your particular example will redefine k.

编辑:


我们不能使用函子,因为我们需要
才能访问
调用的上下文。我们不能使用lambda(snif),因为我们使用旧的编译器

We can't use functors because we need to have access to the context of the call. We can't use lambda (snif) because we use an old compiler

通常的方法是捕获你需要的变量函子,就像一个lambda。 lambda可以做的唯一的事情,函子不能是捕获一切,而不必键入它,但谁写lambda可以看到他们使用的变量,所以这只是方便,他们可以键入它们全部如果他们必须。在您的示例中:

The usual way is to capture whichever variables you need in the functor, just like a lambda does. The only thing a lambda can do that a functor can't is "capture everything" without having to type it out, but whoever writes the lambda can see what variables they use, so that's just convenience, they could type them all out if they had to. In your example:

struct MyFunctor {
    int o;
    MyFunctor(int o) : o(o) {}
    void operator()() const {  // probably not void in practice
        int k = AFunction();
        k++;
        AnotherFunction( k + o );
    }
};

template<typename F>
void DoThings(const F &f) {
    DOSOMETHING(f());
    DOANOTHERTHING(f());
}

int my_function() {
    int o = RandomNumber();
    DoBothThings(MyFunctor(o));
}



您也可以通过引用捕获变量(通常使用指针作为数据成员

You can also capture variables by reference (usually using a pointer as the data member rather than a reference, so that the functor can be copy-assigned).

如果通过上下文,则意味着例如宏参数和/或宏body可能包含 break goto ,因此需要在调用者的词法作用域内, t使用函子或lambda。对于耻辱; - )

If by "context", you mean for example that the macro argument and/or the macro body might contain a break or goto, and hence needs to be inside the lexical scope of the caller then you can't use a functor or a lambda. For shame ;-)

这篇关于宏函数有几行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆