We could omit curly brackets in function declaration?

S

STF

While reading the C++ tutorial in this page:
http://www.cplusplus.com/doc/tutorial/tut2-2.html

I'm astonished to learn that we could omit curly brackets in function
declaration for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly
brackets {}."

But I'm not sure if this is true. At least with GCC under Linux
and MinGW used within Dev-C++ for Windows, I'm unable to compile a file
having something like this:

int test123(int a)
return a;

So, could someone tell me if what's written in that webpage is
correct or not.

TIA

STF
 
V

Victor Bazarov

STF said:
While reading the C++ tutorial in this page:
http://www.cplusplus.com/doc/tutorial/tut2-2.html

I'm astonished to learn that we could omit curly brackets in function
declaration
...definition..

> for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly

Notice the "delimited". Not "surrounded".
brackets {}."

But I'm not sure if this is true. At least with GCC under Linux
and MinGW used within Dev-C++ for Windows, I'm unable to compile a file
having something like this:

int test123(int a)
return a;

So, could someone tell me if what's written in that webpage is
correct or not.

The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

What the author of that tutorial meant by "delimited by curly brackets"
is a puzzle.

V
 
R

Ron Natalie

STF said:
I'm astonished to learn that we could omit curly brackets in function
declaration for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly
brackets {}."
It's not true. The stuff after the parameters in the function
definition is not syntacitally "statement". It is "compound-statement".
A compound statement is a pair of braces with zero or more other
statements inside.

That tutorial seems to have a number of problems.
 
B

ben

The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

Something like this should work:

int f(void)
try
{
//...

return 1;
}
catch (..)
{
//...
return 0;
}

Regards,
ben
 
R

Ron Natalie

ben said:
Something like this should work:

int f(void)
try
{
//...

return 1;
}
catch (..)
{
//...
return 0;
}

And note that try needs a compound statement as well.
 
S

seak.tf

Victor said:
Notice the "delimited". Not "surrounded".


The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

What the author of that tutorial meant by "delimited by curly brackets"
is a puzzle.

V
 
S

seak.tf

Indeed! I've tried the following code with MinGW

int f(void)
try { return 1; }
catch (char * str) { return 0; }

and it compiles but not

int test123(int a)
return a;
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top