CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2011
    Location
    Southampton, UK
    Posts
    9

    Switch statements limited to constants in case clauses?

    On the face of it, it would be handy to be able to list a set of cases which are based on variables rather than consts. This would avoid long if-else statements and clean up some code.

    However, as I understand it, switch statements are “sets” not “lists” meaning the order in which case statements are evaluated is not certain. Therefore variable based case clauses would not be safe.

    Switch statements do support ranges though. E.g. “case 1..4:”. I am not sure if the compiler would object to case ranges overlapping as this would potentially lead to the situation with variable cases i.e. uncertain order of execution.

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Switch statements limited to constants in case clauses?

    Quote Originally Posted by MartinPeterBell View Post
    ...Switch statements do support ranges though. E.g. “case 1..4:”.
    What language supports this?
    Please note that you are in Visual C++ forum.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Switch statements limited to constants in case clauses?

    Quote Originally Posted by VladimirF View Post
    What language supports this?
    Please note that you are in Visual C++ forum.
    IIRC, g++ supports ranges.

    Viggy

  4. #4
    Join Date
    Jun 2011
    Location
    Southampton, UK
    Posts
    9

    Re: Switch statements limited to constants in case clauses?

    Visual Studio C++ supports ranges such as this...

    switch (message)
    {
    case WM_INITDIALOG ... WM_COMMAND:
    return (INT_PTR)TRUE;

    case WM_COMMAND:
    ...
    ...

    And yes I am aware that this is a Visual Studio C++ forum :-)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Switch statements limited to constants in case clauses?

    Quote Originally Posted by MartinPeterBell View Post
    Visual Studio C++ supports ranges such as this...
    Code:
    	switch (message)
    	{
    	case WM_INITDIALOG ... WM_COMMAND:
    		return (INT_PTR)TRUE;
    	case WM_COMMAND:
    ...
    Which version of Visual Studio C++?

    And, please, next time use Code tags while posting code snippets.
    Victor Nijegorodov

  6. #6
    Join Date
    Jun 2011
    Location
    Southampton, UK
    Posts
    9

    Re: Switch statements limited to constants in case clauses?

    My apologies for not being clear (language used and IDE), and not using code tags :-)

    The time I used cases with ranges was indeed via g++ albeit VS 2008 as the IDE.

    I just tried this out on my personal laptop with Visual C++ 2010 Express IDE and standard compiler - the compilation failed. I guess this is a g++ matter and therefore outside the scope of this forum.

    I guess I just didn't consider g++ and Visual C++ could possibly differ so greatly with regards to acceptable C++ syntax.

    Lesson learned :-)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Switch statements limited to constants in case clauses?

    Quote Originally Posted by MartinPeterBell View Post
    I guess I just didn't consider g++ and Visual C++ could possibly differ so greatly with regards to acceptable C++ syntax.

    Lesson learned :-)
    They do not differ if you use the proper ANSI command-line switches in gcc:
    Code:
    -Wall -pedantic
    You will then see that gcc will fail to compile.

    The issue is that by default, gcc enables extensions. Just turn them off and you get a true ANSI C++ compiler.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jun 2011
    Location
    Southampton, UK
    Posts
    9

    Re: Switch statements limited to constants in case clauses?

    Thanks all for talking that through :-)

    Best regards,

    Martin

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured