Search code examples
c++goto

C++ colon syntax with function body


I've encountered an example (for a trivial function-try-block), from which seems to utilize one of the more obscure of aspects of C++ syntax to which (like most obscure aspects of the language) I can not seem to find documentation for.

I've searched the Google to no avail, with all searches returning (rather uselessly) only for constructor initialization (which I'm familiar with), but what I would like to know is the importance of the body: and handler: statements in this constructor block:

class foo{
    foo() try
    //initalizer list that could throw
    {
    body:
       //do stuff that could possibly throw
    } catch(std::exception &e) {
    handler:
       throw;
    }
};

The original author abruptly adds this syntax in a single example and I would like to know the implications of such linguistic constructs


Solution

  • The XXX: is a label. It doesn't serve any functional purpose in the code you posted. It may have been put in by the author to help them organize the code.

    You can use labels with goto statements, as in goto XXX; to jump to that point in your code, although I'll leave it up to you to decide if that is a good think or now.