It seems to me that the two examples below
violate Section 13 of Stroustrup's Annotated Reference Manual.
Probably I am mistaken.
Can someone explain?
(I am using CodeWarrior Development Studio for Mac OS v9)
Example 1:
struct J { public:
J() { m= 0;};
void setVal(float) { m=1;};
void setVal(short) { m=2;};
short m;
};
{
J j1,j2,j3;
j1.setVal(1.0); // compiler errors: ambiguous call...
j2.setVal(1); // ...to overloaded function
j3.setVal((float)1.0); // this call ok
}
why does the compiler not treat '1' as integral,
and "1.0" as floating point?
"an exact match is better than any conversion" - Stroustrup, page 313.
Example 2:
struct K { public:
K() { m= 0; };
void setVal(const float &) { m=1;};
void setVal(short &) { m=2; };
short m;
}
"It is .. possible to distinguish between const T&, volatile T&,
and plain T&"- Stroustrup, page 308.
Nevertheless, in the the following code all four calls to 'setVal'
assign '1' to 'm', indicating that the compiler is treating
bot "1"' and "(short)1"as floating point values!
(if "const float" and plain "float" are distinguishable,
surely "const float" and plain "short" are distinguishable?)
{
K k1,k2,k3,k4;
k1.setVal(1.0);
k2.setVal(1);
k3.setVal((float)1.0);
k4.setVal((short)1);
}
Paul Russell - 05 Mar 2004 00:43 GMT
> Example 1:
> struct J { public:
[quoted text clipped - 15 lines]
>
> "an exact match is better than any conversion" - Stroustrup, page 313.
1.0 is not a float, it's a double. Try 1.0f instead.
Paul
Thorrsten Froehlich - 05 Mar 2004 22:03 GMT
> It seems to me that the two examples below
> violate Section 13 of Stroustrup's Annotated Reference Manual.
[quoted text clipped - 19 lines]
> why does the compiler not treat '1' as integral,
> and "1.0" as floating point?
Turn to section 2.5.1 (page 8) and 2.5.3 (page 10) of the ARM. There
is explained that the constants you are using are "int" in case of the
"1" and "double in case of the "1.0".
BTW, while the ARM is still nice and relevant*, if you want to check
any recent compiler's compliance based on it, you will have a hard
time regarding many of the tricky features of the language. The C++
Programming Language Third/Special Edition together with the ISO C++
standard itself available as PDF from ISO (for something like US$30,
iirc) will be the better choice if you want to evaluate a compiler or
learn the language.
Thorsten
* Unless there has been a new edition published recently, but I am not
aware of any second edition since it came out in 1990 (also there have
been reprints with corrections).