Why the switch statement cannot be applied on strings?

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

int main()
{
    // Compilation error - switch expression of type illegal
    switch(std::string("raj"))
    {
    case"sda":
    }
}

You cannot use string in either switch or case. Why? Is there any solution that works nicely to support logic similar to switch on strings?

22 Answers
22

Leave a Comment