473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Jul 22 '05 #1
5 3727

Andy wrote:
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-

Jul 22 '05 #2

Here is a link online, this explain the reason. However, I did not
understand it well.
http://publib.boulder.ibm.com/infoce...c08cplr403.htm

andy

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

-shez-

Jul 22 '05 #3
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;
}

shez wrote:
Andy wrote:
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-

Jul 22 '05 #4
Please don't top-post. Replies are either interspersed
or appended to the original post. -- rearrranged.

Andy wrote:
shez wrote:
Andy wrote:
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-

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

Jul 22 '05 #5
shez wrote:
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.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Angelina | last post by:
Hi, I need some help in writing a 'if-then-else' statement. I currently have wrote one that looks something like this.. If Combobox1 = xxx and textbox1 <> "" then 'run stored procedure 1 to...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
4
by: Jesper | last post by:
Hi, I would like to issue three statements to the database (mysql v4.1) each time the server starts up. Is there any built in way to do this? Like a init file to put them in? At the moment I...
0
by: Bas Hamer | last post by:
Ok, I'm trying to build a search framework to look trough our propritory logfile format. The logfiles have a dynamic setup so I generate most of my tables on the fly. I currenlty have the code...
3
by: Vinny Vinn | last post by:
I have used simple batch statments with C#.for example: string select = "select model from cars where id = ?;select * from trucks where model = ?"; where ? is a parameter whose value i have...
3
by: subramanian100in | last post by:
I have tried the following in VC++ 2005 Express Edition and g++ in Linux. Consider the class class my_complex { public: my_complex(double r, double i = 10.0) : re(r), im(i) { }...
2
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; void fn(const char * str, char x = 'Y') { cout << "from fn(const char *, char) - " << str << endl; return;
10
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class my_complex { public: friend ostream & operator<<(ostream &os, const my_complex &c);
7
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.