switch/case statement in C++ with a QString type

后端 未结 14 2082
南笙
南笙 2020-12-16 10:19

I want to use switch-case in my program but the compiler gives me this error:

switch expression of type \'QString\' is illegal

How can I us

14条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 10:34

    Late to the party, here's a solution I came up with some time ago, which completely abides to the requested syntax and works with c++11 onwards.

    #include 
    
    bool isStopWord( QString word )
    {
    bool flag = false ;
    
    uberswitch( word )
    {
    case ("the"):
        flag = true ;
        break ;
    case ("at") :
        flag = true ;
        break ;
    case ("in") :
        flag = true ;
        break ;
    case ("your"):
        flag = true ;
        break ;
    case ("near"):
        flag = true ;
        break ;
    case ("all"):
        flag = true ;
        break ;
    case ("this"):
        flag = true ;
        break ;
    }
    
    return flag ;
    }
    

    The only differences to be noticed are the usage of uberswitch in place of switch and the parenthesis around the case value (needed, baucause that's a macro).

    Here's the code: https://github.com/falemagn/uberswitch

提交回复
热议问题