C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

language question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Chris
Guest





PostPosted: Wed Jul 20, 2005 9:53 am    Post subject: language question Reply with quote



Hi,

I found what looks like a bug, but it's more likely something I don't
understand about C++. This code:

const double OneEighty = 180.0;

struct Degrees
{
double m_Degrees;

Degrees( const double degrees )
:
m_Degrees( degrees )
{
}
};

struct Angle
{
double m_Degrees;

Angle(const Degrees & degree)
:
m_Degrees( degree.m_Degrees )
{
}

Angle & operator+=(const Angle & rhs)
{
m_Degrees += rhs.m_Degrees;

return *this;
}
};

void test(void)
{
Degrees degrees180( OneEighty );

Angle angle1( Degrees(180.0 ));// an instance of Angle
Angle angle2( Degrees(OneEighty));// declaration of a func??

//
// The following line fails when uncommented
// on the compilers on my system of the following versions:
// gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-126)
// gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
// gcc version 3.4.0 (Red Hat Linux 3.4.0-1)
// gcc version 4.0.0
// gcc version 4.0.1 20050617 (prerelease)
//
angle1 += angle2; // won't compile because angle2 isn't an Angle
//...
}

What's wrong with this? If I substitute "degrees180" for
"Degrees(OneEighty)" the line below compiles OK.

The difference in the declaration of angle1 and angle2 seems fairly
minor -- a literal constant versus a constant value -- but the compiler
thinks so.

thanks

Chris Arena
Portsmout, RI

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
tony_in_da_uk@yahoo.co.uk
Guest





PostPosted: Wed Jul 20, 2005 2:08 pm    Post subject: Re: language question Reply with quote



Hi Chris,

Your comment "declaration of a function" shows you already suspected
the actual problem... angle2 is declared as a function
"Angle(*)(Degrees)". If I remember coorrectly, the () around OneEighty
are optional in naming the function parameter. It's silly, but that's
life. There's a discussion of the problem in one of those common C++
books - probably one of Herb Sutter's.

Cheers,

Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Wed Jul 20, 2005 3:04 pm    Post subject: Re: language question Reply with quote



Chris <ccarena (AT) cox (DOT) net> writes:

Quote:
I found what looks like a bug, but it's more likely something I
don't understand about C++.

Indeed. Rest assured that the issue is subtle enough to catch those
who think they understand C++ well from time to time.

Next time please copy&paste the actual error message(s) into your
post.


Quote:
const double OneEighty = 180.0;

struct Degrees
{
double m_Degrees;

Degrees( const double degrees )
:
m_Degrees( degrees )
{
}
};

[snip]

Quote:
void test(void)
{
Degrees degrees180( OneEighty );

Angle angle1( Degrees(180.0 ));// an instance of Angle
Angle angle2( Degrees(OneEighty));// declaration of a func??

This line is syntactically equivalent to

Angle angle2(Degrees OneEighty);

and thus means the declaration of a function angle2 that takes a
parameter of type Degrees and returns an Angle object.

[Whenever a statement could in principle be both a definition and a
declaration (that is not a definition), the compiler has to parse it
as a declaration that is not a definition.]


Quote:
//
// The following line fails when uncommented
// on the compilers on my system of the following versions:
// gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-126)
// gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
// gcc version 3.4.0 (Red Hat Linux 3.4.0-1)
// gcc version 4.0.0
// gcc version 4.0.1 20050617 (prerelease)
//
angle1 += angle2; // won't compile because angle2 isn't an Angle

Since you probably haven't overloaded += for a lefthand side argument
of type Angle and a righthand side arguemnt of type Angle(Degrees),
the compiler won't accept this line.


Quote:
}

What's wrong with this? If I substitute "degrees180" for
"Degrees(OneEighty)" the line below compiles OK.

Angle angle2(degrees180);

can only be parsed as the definition of a local variable, so that's
what it is.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Jul 20, 2005 3:05 pm    Post subject: Re: language question Reply with quote

Chris wrote:
Quote:
const double OneEighty = 180.0;

struct Degrees
[...]
struct Angle
[...]
Degrees degrees180( OneEighty );

Angle angle1( Degrees(180.0 ));// an instance of Angle
Angle angle2( Degrees(OneEighty));// declaration of a func??

The rule given in the standard is that when something can be parsed as a
declaration, it is a declaration. Yes, countless others did and still do
stumble across this. The above is equivalent to
Angle angle2( Degrees OneEighty);
The workaround is to add additional brackets
Angle angle2( (Degrees(OneEighty)));
or use static_cast.

Quote:
// gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-126)

Just FYI: http://gcc.gnu.org/gcc-2.96.html

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.