switch on std::string

This is a discussion on switch on std::string within the C++ Programming forums, part of the General Programming Boards category; I know that this is not possible in standard C++, and I have seen no mention of it in the ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351

    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
    2,351
    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
    21,602
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21