Search code examples
c++qtmacrosqstringqtcore

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 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


Solution

  • 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

    • Returning since that will not have the logic of "return value of macro", but will actually return in the outter function. 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.

    • 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.

    • You are passing std::string rather than 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.