Why does switch() handle ONLY int-values

This is a discussion on Why does switch() handle ONLY int-values within the C++ Programming forums, part of the General Programming Boards category; Hi! I was thinking the other day: Why does switch() handle ONLY int-values? Wouldn't it have been really easy to ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    Why does switch() handle ONLY int-values

    Hi!
    I was thinking the other day: Why does switch() handle ONLY int-values? Wouldn't it have been really easy to implement at compiler level that all types that have a == operator (which returns a bool) are suitable for switching?
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes its possible (PHP allows you to switch on pretty much anything) but it would lose a lot of its efficiency. A switch is generally done using a jump table where the value of variable tells you the exact offset to go to which then tells the program where to jump to. A lot more efficent then basically an if else string that it would become if you had non integer types.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 10:06 AM

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