简体   繁体   English

在cpp中用#define定义函数?

[英]define function with #define in cpp?

i want check sth in if multiple time, i use #define for this ad i want to use it but it doesn't work 我想检查是否多次,我为此广告使用#define,但我无法使用

#define CHECK_CONDITION(QString condition, start, curr) if(condition == "" ) return true; return (table.commonIn() == "team_id"? list()[start]->team() ==list()[curr]->team() :list()[start]->team() ==list()[curr]->team())

and i use it like this : 我这样使用它:

if(CHECK_CONDITION(table.commonIn().toStdString(), start, start-idx);) {
   findFalse = true;
}

how can i use this define in my code/ thank you in advance 我如何在我的代码中使用此定义/提前谢谢

Preprocessor has no notions of types, so you when you declare a #define with types, you do not need to specify parameter type: 预处理程序没有类型的概念,因此您在使用类型声明#define时,无需指定参数类型:

#define CHECK_CONDITION(condition, start, curr) { if(condition == "" ) return true; return (table.commonIn() == "team_id"? list()[start]->team() ==list()[curr]->team() :list()[start]->team() ==list()[curr]->team())}

Besides, a #define is expanded where you use it (the preprocessor replaces CHECK_CONDITION with that block of code), so your code won't compile because of at least one reason: you will be nesting an if inside an if condition, which is a syntax error. 此外,一个#define在您使用它(预处理器替换展开CHECK_CONDITION你会被嵌套的:与代码块),因此您的代码不会因为至少一个原因编译if内部的if条件,这是语法错误。

Use a (maybe inline) function, instead: 使用(可能是内联)函数,而不是:

inline
bool check_condition(QString condition, int start, int curr) {
    if(condition == "" ) return true;
    return (
        table.commonIn() == "team_id"?
            list()[start]->team() == list()[curr]->team():
            list()[start]->team() == list()[curr]->team()
    )
}

This also makes explicit a possible syntax error here: I don't know what you meant with the last two lines, so I left that untouched... 这也使此处可能出现语法错误:我不知道您对最后两行的含义,因此我保持不变。

My 2cents: you should see the preprocessor in C++ as a last resort: you have templates, const variables and inline functions. 我的2cents:您应该将C ++中的预处理器作为最后的选择:您拥有模板,const变量和内联函数。

The main reason why it was left in C++ (instead of using an include keyword alone or the like) is to keep backwards compatibility with C. Never use the preprocessor, unless any other solution is fairly over-complex. 之所以将其保留在C ++中(而不是单独使用include关键字等),主要是为了保持与C的向后兼容性。切勿使用预处理器,除非任何其他解决方案都过于复杂。

A #define in C/C++ is a macro definition that is a simple textual replacement. C / C ++中的#define是宏定义,它是简单的文本替换。 This definition is more like a function and is attempting to assign a type to one of the parameters. 该定义更像一个函数,并且正在尝试将类型分配给参数之一。 This isn't legal syntax and hence the QString portion needs to be removed 这不是合法的语法,因此需要删除QString部分

Overall though this code is not ideally suited for a macro. 总的来说,尽管此代码并不理想地适合于宏。 The arguments start and curr are both used multiple times in the expansion. 扩展中多次使用了参数startcurr This means that if a side effecting expression is passed to the macro it will be executed potentially many times. 这意味着,如果将副作用表达式传递给宏,则可能会执行多次。 A function would be much more appropriate here 这里的功能会更合适

You could use this modified macro: 您可以使用此修改后的宏:

#define CHECK_CONDITION(condition, start, curr) \
    if(condition == "" || (condition == "team_id"? list()[start]->team() ==list()[curr]->team() :list()[start]->team() ==list()[curr]->team())

The mistake you have made in this macro: 您在此宏中犯的错误:

  • Specifying QString for the condition 为条件指定QString

  • Returning since that will not have the logic of "return value of macro", but will actually return in the outter function. 从那以后返回将不具有“宏返回值”的逻辑,但实际上将在outter函数中返回。 That is because it has to go through the preprocessor step. 那是因为它必须经过预处理器步骤。

  • You do not need separate branches within the macro as a simple logical OR ("||") can do it. 您不需要在宏内使用单独的分支,因为简单的逻辑或(“ ||”)可以做到。

  • You used the table common in string getter within the macro even though you already passed the condition variable. 即使已经传递了条件变量,您仍在宏中使用了字符串获取器中的公用表。

  • I would use the back-slash to make it split into pieces for better readability. 我将使用反斜杠将其拆分为多个部分,以提高可读性。

and then you could keep the rest of your code like this: 然后您可以像这样保留其余代码:

if(CHECK_CONDITION(table.commonIn(), start, (start-idx))) {
   findFalse = true;
}

The mistakes here you made: 您在这里犯的错误:

  • You had a needless semicolon within the if condition which is invalid C++ syntax. 您在if条件中使用了不必要的分号,这是无效的C ++语法。

  • You could get into troubles in general (not here) with not putting the subtraction into bracket. 如果不将减法放在括号内,通常会遇到麻烦(不在此处)。

  • It would be cleaner if you could make two separate variables for the string and the current integer before the CHECK_CONDITION macro call as demonstrated below. 如果可以在CHECK_CONDITION宏调用之前为字符串和当前整数创建两个单独的变量,这将更加干净,如下所示。

  • You are passing std::string rather than QString. 您正在传递std :: string而不是QString。

But it would be even nicer if you could simply the second part like this: 但是,如果您可以像下面这样简单地讲第二部分,那就更好了:

QString myString = table.commonIn();
int curr = start - idx;

if(CHECK_CONDITION(myString, start, curr)) {
   findFalse = true;
}

Disclaimer: I have been trying to make your macro and its caller working, but in general try to avoid macros when possible. 免责声明:我一直在尝试使您的宏及其调用程序正常运行,但通常在可能的情况下尽量避免使用宏。

There are cases where they make sense, but there are alternatives like template (which is not applicable here) or inline functions and methods (not sure if applicable in here). 在某些情况下,它们是有意义的,但是还有其他选择,例如模板(此处不适用)或内联函数和方法(不确定是否适用于此处)。 Depending on your use case, you can pick up whichever you prefer, but you can get the point of this answer how to make the macro working for your use case. 根据您的用例,您可以选择自己喜欢的任何一个,但是您可以得到此答案的重点,即如何使宏适用于您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM