Thread: switch on std::string

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    switch on std::string

    I know that this is not possible in standard C++, and I have seen no mention of it in the proposed new standard, but I was thinking it might be cool if they extended the functionality of switch to allow anything that implements the equality operator. this would require that each case needs to implicitly convert to the switched type, which is no big deal for std::string, as you all know, because const char pointers implicitly convert to std::string.

    I'd love to hear comments on this.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Switch is largely a crutch in programming (often meaning hard-coding code-paths and code reduplication). As such I don't see any reason to widen the usability of switch (as you can already use if...else chains).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by anon View Post
    Switch is largely a crutch in programming (often meaning hard-coding code-paths and code reduplication). As such I don't see any reason to widen the usability of switch (as you can already use if...else chains).
    I agree that if ... else if ... else works just fine, but there are many cases where there are a well-defined set of code paths, and a switch is a more convenient, and in many cases, more easily understood way to achieve that functionality.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why? Because you can forget the break?

    With enum's switches have the advantage that the compiler can check whether the switch covers all enumeration values but with std::string and other types I don't see any clear advantage.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I have to agree that it would be a quite valuable addition to the language. I've had once or twice that it'd've been very useful (for instance, parsing something like HTTP, and you have to find out the method, a switch with cases "GET", "POST", etc would imho be better than an if-else sequence. I've seen HTTPd's switch on the length of the method to find it fast enough). But for something like that, it would need at least an operator<, not just an operator=.

    That is also why I like switches as well; they can easily be optimized by the compiler. Using lookup tables, or binary searched, or whatever. If-else sequences would be a lot tougher to be optimized to something similar.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    That is also why I like switches as well; they can easily be optimized by the compiler. Using lookup tables, or binary searched, or whatever. If-else sequences would be a lot tougher to be optimized to something similar.
    Isn't that "easily be optimized by the compiler" part due to the very restrictions that exist in the first place? Like, if the requirement was relaxed to any type for which operator== is defined, then such optimisations would no longer be generally applicable.

    Yet, if a programmer really wants to lookup with time comparable to "binary search", a std::map or std::multimap would be appropriate. If a programmer wants constant time lookup, perhaps a hash table with std::tr1::unordered_map or std::tr1::unordered_multimap would be appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM