What's the meaning of this sentence, "0;"?

F

fadics

0;
The above sentence can be compiled. What does it mean? Why C++ can compile
such sentence?
 
G

Gianni Mariani

fadics said:
0;
The above sentence can be compiled. What does it mean?

"Compute the expression '0'". It does nothing other than place some
code in the way.
Why C++ can compile
such sentence?

The C++ grammer allows it.

It just does !
 
A

Alf P. Steinbach

* Gianni Mariani:
"Compute the expression '0'". It does nothing other than place some
code in the way.


The C++ grammer allows it.

It just does !

One might say it would be better if C++ had an explicit notion of
"command". It doesn't. Neither does C#, and there leads to very
awkward and implicit formulations in the standard, instead of simple
and explicit ones (although C# does not permit a command like '0;').

And it all hangs together.

We have "functions" that return "void". And we have not only conceptual
problems because of that, but also technical ones such as overloading.
In short, it indicates a language that evolved more than was designed,
and like our own human bodies therefore has many nonsensical constructs.

The upside is that as an evolved language it's splendidly adapted to its
most common role and environment, probably much more than a one designed
from scratch would be (imagine designing the human body from scratch!).

Hope this helps the OP,

- Alf
 
B

Bogdan Sintoma

fadics said:
0;
The above sentence can be compiled. What does it mean? Why C++ can compile
such sentence?

It is an "expression statement" (paragraph 6.2. in the c++ standard).
Also, you can have a null statement.

What about:
inline int foo() { return 0; }
void bar(){
foo();
}
Should a c++ compiler complain about the above code?
 
O

Old Wolf

Alf said:
We have "functions" that return "void". And we have not only
conceptual problems because of that, but also technical ones
such as overloading. In short, it indicates a language that
evolved more than was designed, and like our own human bodies
therefore has many nonsensical constructs.

You think there is something wrong with having a function
that returns void ?!
 
A

Alf P. Steinbach

* Old Wolf:
You think there is something wrong with having a function
that returns void ?!

Nothing wrong with a routine that doesn't return a result; much
wrong with a "function" that "returns" a "void"... :)

Filling in that ellipsis: one reason we have functions returning
void is that a function call, or more generally an expression, is
a valid command (if C++ had a syntactical notion of command; I'm
too lazy to look up the grammar in order to define it, but loosely,
a statement that isn't a declaration). That has all sorts of
consequences, not the least of which is that the return type is not
part of the function signature wrt. e.g. overloading. And that's,
IMHO, bad; I prefer to think of how it should have been as

C++ Equivalent in Foo++ (a hypothetical lang.)
int f() r( out int x ); def f = func(r, x);
void f() r()

In the first line, C++ has no argument that helps in overloading,
wheras Foo++ has. In the second line, C++ has a dummy "argument" (the
function result), which of necessity must be void, whereas Foo++ simply
has no argument. I think Foo++ is better, especially if it existed.
 
S

Sa7an1c

The definition of a function is something that maps an input to the
corresponding output... The void return is not a function because of
this technicality.
 
R

Richard Herring

In message said:
The definition of a function is something that maps an input to the
corresponding output...

That's not a definition: you haven't said what you mean by "input" and
"output".
The void return is not a function because of
this technicality.
It's a mapping from one set to another set. Nothing there says that the
second set can't be the empty set ;-)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top