a little syntax goes a long way??

C

cppaddict

It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

Thanks,
cpp
 
T

Thore Karlsen

It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

Because you can immediately declare an instance of a class, even while
leaving the class anonymous:

class
{
int x;
} instance;
 
L

Leor Zolman

It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

Thanks,
cpp

Thore's reason works for me...but there's also this: The C++ Standard
defines the syntax of a function definition (8.4/1) as follows:

Function definitions have the form
function-definition:
decl-specifier-seqopt declarator ctor-initializeropt function-body
decl-specifier-seqopt declarator function-try-block

function-body:
compound-statement

Note the final "compound-statement". Compound statements don't end in
semi-colons! And the compound statement "part" of a function definition
(the "function-body") isn't the entire function definition; that's why a
function definition is "not considered a statement".
-leor
 
J

jeffc

cppaddict said:
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I think the "conceptual difference" lies in the meaning of "definition".
Classes are kind of funny when it comes to the meaning of "definition".
When you declare a function, you use a semicolon at the end. When you
define a function, you don't. But the "definition" of a function is really
an "implementation". Note that when you actually implement the class, you
need no semicolon. So a class "definition" is really more of a
"declaration", hence the need for the semicolon.
 
A

Andrey Tarasevich

cppaddict said:
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

Maybe yes, maybe no. Most likely - no.
I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

From the purely syntactical point of view, the fact that a semicolon is
used to "end a statement" is not really reflected in C++ grammar.
Moreover, as Leor already pointed out, this is not always the case.

If you take a look at the relevant portion of the grammar (see 6.7/1 and
next 7/1) you'll see that no semicolons are mentioned at this
grammatical level. Even simple statements don't have "their own"
semicolons. Instead, they "inherit" them from lower levels of the
grammar. For example, semicolons after declarations come with the last
rule in 7/1 - the rule for 'simple declaration'. Note that function
definition is not a 'simple declaration', while class definition is a
'simple declaration'. That's why there are no semicolons after function
declarations.

BTW, member function definitions can be optionally followed by
semicolons, but this is again their own "personal" semicolons introduced
by a separate grammatical rule in 9.2.
If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

The rationale is rather well-known. But a "larger conceptual
difference"... I don't think it exists.
 
J

jeffc

Andrey Tarasevich said:
Note that function
definition is not a 'simple declaration', while class definition is a
'simple declaration'. That's why there are no semicolons after function
declarations.

Function definition? Declaration?
 
A

Andrey Tarasevich

jeffc said:
Function definition? Declaration?
...

In the last quoted sentence I meant to say "function definition", of
course, i.e. "function declaration, which is a definition". Hopefully,
it is clear from the context.
 

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
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top