Why the switch statement cannot be applied on strings?

前端 未结 20 2603
离开以前
离开以前 2020-11-22 03:58

Compiling the following code and got the error of type illegal.

int main()
{
    // Compilation error - switch expression of type illegal
    sw         


        
20条回答
  •  攒了一身酷
    2020-11-22 04:36

    Late to the party, here's a solution I came up with some time ago, which completely abides to the requested syntax.

    #include 
    
    int main()
    {
        uberswitch (std::string("raj"))
        {
            case ("sda"): /* ... */ break;  //notice the parenthesis around the value.
        }
    }
    

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

提交回复
热议问题