Why C++ has ambiguity between expresion statments and declaration statments?

A

Andy

Hi,

How can a statment be explainted as expression and declaration at the
same time? Then how shall we resolve this ambiguity?
Thanks a lot!

andy
 
S

shez

Andy said:
Hi,

How can a statment be explainted as expression and declaration at the
same time? Then how shall we resolve this ambiguity?
Thanks a lot!

andy

Can you give an example? The C++ language is not supposed to be
ambiguous.

-shez-
 
A

Andy

An example is as follows:
void f(int x)
{
x++;
};

class Int
{
public:
Int() { val = 0; }
private:
int val;
};

int main()
{
int (x);
Int(y);
x = 5;
f (x);
return 0;
}
 
T

Thomas Matthews

Please don't top-post. Replies are either interspersed
or appended to the original post. -- rearrranged.
An example is as follows:
void f(int x)
{
x++;
};

This is a great function that doesn't do anything.
Since the variable 'x' is passed by value, the
function is incrementing a _copy_ of the variable,
not the original variable. Also, it doesn't return
any value.

Perhaps you want something like this:
void f(int &x) // pass by reference
{
x++; // Now it modifies the original variable.
} // No need for a semicolon, ';', here.

class Int
{
public:
Int() { val = 0; }
private:
int val;
};

One could easily have used:
typedef int Int;

int main()
{
int (x);
Int(y);

I believe you want:
int x;
Int y;

x = 5;
f (x);

Remember, f(x) doesn't modify its argument.
See above.
return 0;
}

Remember that C++ is a case-sensitive language.
The identifiers 'Book' and 'book' are different.
This is perfectly legal:
class Book {};
Book book;

However, this kind of style is frowned upon since there
is greater chance to confuse the reader.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

Ron Natalie

shez said:
Can you give an example? The C++ language is not supposed to be
ambiguous.
There is an ambituity in the C++ GRAMMAR between declarations
and expressions at times. The language itself is NOT ambiguous.
The grammar ambiguity is resolved by semantically assuming any
ambiguous constuct is a declaration.

An example:
T foo(int(x));

is the "int(x)" an expression (a conversion of x to int) that is the
initializer of the foo object or is it a declaration of a function argument
called x making the whole thing a function declaration.

The language semantic rules resolve it to a declaration.
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top