Stupid compiler

J

JKop

int main()
{
float blah( float() );

blah = 5.2;
}


Why the hell does g++ think that that's a function declaration?!

It's telling me that I can't assign a value to a function!

Is this a bug in g++?


-JKop
 
M

Marco Manfredini

JKop said:
int main()
{
float blah( float() );

blah = 5.2;
}


Why the hell does g++ think that that's a function declaration?!

Return Type
| Function name
| | Argument Type (a parameterless fn that returns a float)
| | |
V V V
float blah ( float() )

C++ Rule In Case Of Ambiguities:
"If it Looks like declaration, it is a declaration."

Write 'float blah=float();' instead. It means exactly the same as your
intent was with 'float blah(float())'

Marco
 
J

John Harrison

int main()
{
float blah( float() );

blah = 5.2;
}


Why the hell does g++ think that that's a function declaration?!

It's telling me that I can't assign a value to a function!

Is this a bug in g++?


-JKop


No, remember parameter names are optional in C++.

float blah(float x);

same as

float blah(float (x));

same as

float blah(float ());

Try this

float blah = float();

No confusion possible.

john
 
J

John Harrison

Return Type
| Function name
| | Argument Type (a parameterless fn that returns a float)
| | |
V V V
float blah ( float() )

C++ Rule In Case Of Ambiguities:
"If it Looks like declaration, it is a declaration."

Write 'float blah=float();' instead. It means exactly the same as your
intent was with 'float blah(float())'

Marco

I tested it, your interpretation is right, mine is wrong.

float blah(float (x));

is a function taking one float as an argument, but when x is removed the
parens change meaning and become part of the type of the parameter to blah.

Change it to

float blah(float (()));

And then both interpretations of the parens are present.

john
 
A

Ali Cehreli

No, remember parameter names are optional in C++.

float blah(float x);

same as

float blah(float (x));

So far so good.
same as

float blah(float ());

I don't think so. The parameter is a function pointer in this
case. (I couldn't have possibly known this until I tried. :)

Ali
 
T

tom_usenet

int main()
{
float blah( float() );

blah = 5.2;
}


Why the hell does g++ think that that's a function declaration?!

It's telling me that I can't assign a value to a function!

Is this a bug in g++?

In addition to the other posts, another version is:

float blah((float()));

which is even more ugly. Note that didn't work prior to GCC 3.4, and
stands as the one and only GCC bug report I have made!

Tom
 
J

JKop

tom_usenet posted:
In addition to the other posts, another version is:

float blah((float()));

which is even more ugly. Note that didn't work prior to GCC 3.4, and
stands as the one and only GCC bug report I have made!

Tom

....and a hope you gave me credit!


-JKop
 

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,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top