CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: fallthrough

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    fallthrough

    I'm building a project which needs to get built with VC2005 for various reasons. One of the files has a switch statement looking like this:-

    Code:
    		switch (argc) {
    			case 9:
    				if (types[8] == 'f') {
    					linkid = (int) argv[8]->f;
    				} else {
    					linkid = argv[8]->i;
    				}
    			case 8:
    				if (types[7] == 'f') {
    					linkset = (int) argv[7]->f;
    				} else {
    					linkset = argv[7]->i;
    				}
    			case 7:
    				if (types[6] == 'f') {
    					port = (int) argv[6]->f;
    				} else {
    					port = argv[6]->i;
    				}
    
    				// etc
    		}
    Notice that there are no break; statements in between each case. Someone else builds the same project with gcc and he's added some [[fallthrough]] lines at the end of each case - i.e.


    Code:
    		switch (argc) {
    			case 9:
    				if (types[8] == 'f') {
    					linkid = (int) argv[8]->f;
    				} else {
    					linkid = argv[8]->i;
    				}
    				[[fallthrough]];
    			case 8:
    				if (types[7] == 'f') {
    					linkset = (int) argv[7]->f;
    				} else {
    					linkset = argv[7]->i;
    				}
    				[[fallthrough]];
    			case 7:
    				if (types[6] == 'f') {
    					port = (int) argv[6]->f;
    				} else {
    					port = argv[6]->i;
    				}
    				[[fallthrough]];
    
    				// etc
    		}
    I don't know if later versions of MSVC would accept this but VC2005 doesn't (giving me:- error C3409: empty attribute block is not allowed).

    Obviously I could just comment out the lines but I just wondered if fallthrough is a valid keyword of some kind later versions of C++ ?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: fallthrough

    Quote Originally Posted by John E View Post
    but I just wondered if fallthrough is a valid keyword of some kind later versions of C++ ?
    It's a standard "attribute specifier sequence" in C++ 17,

    https://en.cppreference.com/w/cpp/language/attributes

    I had no idea this feature even existed but I cannot be the only one since no one knows all of C++ except Bjarne.
    Last edited by wolle; June 20th, 2018 at 05:04 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: fallthrough

    Yes, there's lots of 'goodies' in c++17! Unfortunately, for pre c++17 compilers, you can't even define a null macro for [[fallthrough]] as an identifier can't start with a [ Doh!

    For info about c++17 and MS, see https://blogs.msdn.microsoft.com/vcb...he-c-standard/

    Also see http://en.cppreference.com/w/cpp/com....2B17_features for details of what's in the various c++ standards (including so far in the new c++20) and which compilers/versions support them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: fallthrough

    Thanks guys - it looks like C++17 is supported from VS2015 onwards so I guess I could change all instances of [[fallthrough]] to fall_through and then do something like this:-

    Code:
    #if _MSC_VER < 1900
    #define fall_through
    #else
    #define fall_through [[fallthrough]]
    #endif
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: fallthrough

    it looks like C++17 is supported from VS2015 onwards
    Not quite. Only a quite small part of VS2017 is implemented in VS2015. To get the full c++17 implementation, you need the latest version of VS2017 (15.7). Even early versions of VS2017 didn't fully support c++17!

    [[fallthrough]] is only implemented in VS2017.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: fallthrough

    Failthroughs [sic] are difficult to maintain and debug. Is there no other way to structure the code?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: fallthrough

    Quote Originally Posted by Arjay View Post
    Failthroughs [sic] are difficult to maintain and debug. Is there no other way to structure the code?
    But even using case fall-throughs, the code can be simplified as

    Code:
    switch (argc) {
        case 9: linkid = (types[8] == 'f') ? (int)argv[8]->f : argv[8]->i;
        case 8: linkset = (types[7] == 'f') ? (int)argv[7]->f : argv[7]->i;
        case 7: port = (types[6] == 'f') ? (int)argv[6]->f : argv[6]->i;
    
        // etc
    }
    which makes it much more obvious about the 'fall-through' - even without [[fallthrough]] being used. IMO actually using [[fallthrough]] in this simplified code would detract from the code readability.
    Last edited by 2kaud; June 20th, 2018 at 12:49 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: fallthrough

    Years ago I wrote argument parsing classes for c++ and c#. An instance of the class describes the header, footer, and arguments. The end result is consistent, self documenting (via /?) cmd arguments.

    Any cmd tools utilities I write use the same classes and it's easy to setup and one less thing for me to debug.

  9. #9
    Join Date
    Feb 2017
    Posts
    677

    Re: fallthrough

    Quote Originally Posted by Arjay View Post
    Failthroughs [sic] are difficult to maintain and debug. Is there no other way to structure the code?
    One way would be to use a simple "state machine" implementation, like
    Code:
    	int state = args; // start state
    	while (state > 0) {
    		switch (state) {
    			case 9:
    				// do something in this state
    				state = 8; // goto some other state
    				break;
    			case 8:
    				// do something in this state
    				state = 7; // goto some other state
    				break;
    			case 7:
    				// do something in this state
    				state = 0; // exit
    				break;
    			default: // error - state missing
    				state = 0; // exit
    				break;
    		}
    	}
    Not much of a difference maybe but there's no fallthrough.
    Last edited by wolle; June 21st, 2018 at 03:24 AM.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: fallthrough

    if I'm reading the code right, you could consider something like

    Code:
    	const char types[] = "fzfe ffyf";
    
    	int var1, var2, var3, var4, var5, var6, var7, var8, var9, var10;
    
    	// Array of pointers to variables to be set
    	int *pvar[] = {&var1, &var2, &var3, &var4, &var5, &var6, &var7, &var8, &var9, &var10};
    
    	const size_t st = sizeof(types) / sizeof(types[0]);
    	const size_t sa = sizeof(pvar) / sizeof(pvar[0]);
    
    	static_assert(st == sa, "Sizes of the arrays not the same");
    
            // Set the required variables
    	for (size_t a = std::min(argc, sa); a > 0; --a)
    		*pvar[a - 1] = (types[a - 1] == 'f') ? (int)argv[a - 1]->f : argv[a - 1]->i;
    which should be OK with VS2005?

    But if I recall your other posts, you're just trying to get code given to you to work with VS?
    Last edited by 2kaud; June 20th, 2018 at 05:22 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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