Multi-statement Macros in C++

前端 未结 7 1417
轻奢々
轻奢々 2021-01-19 08:00

In C++, is it possible to make a multi-statement macro with nested if statements inside of it like the one below? I\'ve be

7条回答
  •  不要未来只要你来
    2021-01-19 08:50

    If you're using C++ you should avoid using macros altogether. They are not type-safe, they're not namespace-aware, they're hard to debug and just they're plain messy.

    If you need a type-independent function, use templates:

    template 
    bool match_symbol(T symbol, T token) {
        if(something == symbol){
            if( symbol == '-'){
            }else if (symbol != '-'){
            }
        ...
    

    or if the parameters can be different types:

    template 
    bool match_symbol(T symbol, V token) {
        if(something == symbol){
            if( symbol == '-'){
            }else if (symbol != '-'){
            }
        ...
    

提交回复
热议问题