goto label inside of if statement

Discussion in 'C++' started by W Karas, Jan 23, 2014.

  1. W Karas

    Jax Guest

    Paavo.... Gareth tells me that your advice is reliable. I have made a note of
    it. Thank you!
     
    Jax, Jan 27, 2014
    #21
    1. Advertisements

  2. W Karas

    W Karas Guest

    I'm honored, but you are too kind.
    This is a situation where Pascal's nested functions where could be very advantageous. The nested function has access to the containing function's scope, so it is equivalent to a named block where "return" is a break from anylevel of nesting. Perhaps nested function support should be added to C++ (and C) since the hidden overhead (hidden pointer to containing function's scope) is straight-forward.

    I believe the Standard already has one precedent for recommending a standard practice, namely, reserve symbols starting with underscore ( _ ) for standard library header files. Maybe this is a case where another such recommendation is needed. Perhaps that labels should begin with EXIT_BLOCK_ if and only if they are used exclusively to do a break from with a nested block.That has advantages over my tricky macros.
     
    W Karas, Jan 27, 2014
    #22
    1. Advertisements

  3. * W Karas:
    The following is valid C++, as of C++11:


    Code (Text):

    #include <iostream>
    #include <math.h>       // sqrt
    using namespace std;

    auto main()
    -> int
    {
    int const max_a         = 12345;
    int const max_numbers   = 42;

    int n = 0;
    [&]() -> void
    {
    int prev_c  = 0;
    for( int a = 1; a <= max_a; ++a )
    {
    for( int b = 1; b < a; ++b )
    {
    int const c_sq = a*a + b*b;
    int const c = int( sqrt( c_sq ) + 0.5 );
    if( c*c == c_sq )
    {
    if( c < prev_c )
    {
    cout << (n > 0? " " : "") << c;
    ++n;
    if( n == max_numbers )
    {
    return;
    }
    }
    prev_c = c;
    }
    }
    }
    }();
    cout << endl;

    cout << "Found " << n << " interesting numbers." << endl;
    }
     
    Exactly how it implements references to local variables is up to the
    compiler, but a smart compiler would use a pointer to the stack frame.
    Unlike Pascal's nested function a C++ lambda can alternatively capture
    by value.

    I would however use a named function instead of an inline lambda.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Jan 27, 2014
    #23
  4. W Karas

    W Karas Guest

    If a hypothetical nested function in C++ were (implicitly or explicitly) inline, then assuming good optimization, any overhead issues would be moot.
    If the (named) function substituted for the block were declared inline, then the "problem" would be the need to make explicit the block's dependence on local variables in the parameter list. Good optimization would hopefullymake object level overhead issues moot in this case too.
     
    W Karas, Jan 27, 2014
    #24
  5. Please fix the quoting before posting.

    - Alf
     
    Alf P. Steinbach, Jan 27, 2014
    #25
  6. W Karas

    W Karas Guest

    Please fix the quoting before posting.
    I have not found an alternative to using the groups.google.com web portal for usenet. I don't have enough time to properly follow this group, much less correct the mess that Google makes when quoting the original post. Is it best to simply delete all or most of the original post one is responding to when using the Google portal?
     
    W Karas, Jan 27, 2014
    #26
  7. W Karas

    red floyd Guest

    Use eternal-september.org
     
    red floyd, Jan 28, 2014
    #27
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.