Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this C++ function definition not require curly braces? [duplicate]

I was reading Stroustrup's "Programming -- Principles and Practice Using C++" and found that he included a function without the main curly braces without explaining himself and online people say it is impossible.

I have compiled the code and it works totally fine.

void f()
    try {} 
    catch(...) {}

I expect to get a compiler error from this but I do not and it works fine. I am using C++17.

like image 999
csgosmorf Avatar asked Jul 02 '19 15:07

csgosmorf


1 Answers

not require curly braces?

There are curly braces:

void f() try {} catch(...) {}
             ^^

This is a Function-try-block:

A function-try-block associates a sequence of catch clauses with the entire function body

like image 159
Paul Evans Avatar answered Sep 25 '22 00:09

Paul Evans