Article View: comp.std.c++
Article #152Re: Integral promotions
From: Steve Clamage
Date: Sat, 12 Sep 1992 17:47
Date: Sat, 12 Sep 1992 17:47
45 lines
2079 bytes
2079 bytes
jason@jarthur.claremont.edu (Jason Merrill) writes:
>Why is the conversion int -> long (or unsigned int -> unsigned long) not
>considered an integral promotion?
>I am in the situation of trying to take either a pair of longs (with
>default arguments) or a double for creation of a Fraction. If I do
>something like
>Fraction(1),
>my compiler (BC++ 3.0) complains about ambiguity. This seems wrong to me.
The "promotions" are from "smaller" types to int or unsigned int.
There are automatic conversions to long and double. This is the
heritage from C, unchanged in C++. That isn't much of a "why",
but I don't have a better answer.
In contrast to your question, some programmers complain about having
so many silent conversions, some of which they feel ought to be errors.
No conversion is preferred over any other conversion when resolving
a call to an overloaded function. If you have only
foo(long)
foo(double)
and call foo() with an int argument, neither function is preferred
over the other, and the call is ambiguous.
You generally want to provide an overloaded function taking an int
parameter if you have any taking any other integral types, and
one taking a double parameter if you have any taking other
floating types. You can do this:
inline T foo(int i) { return foo((long)i); } // foo(long) returns type T
(Hmmm. T foo(long) sounds like a beverage.)
Make this NON-virtual in the base class, and don't override it in
any derived class. You will then always get the correct version
of a virtual foo(long) every time you call foo with a (signed)
integer type, with no extra overhead.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16
Technical Details
Message-ID:
<1992Sep12.174705.6381@taumet.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!taumet!steveReferences:
<1992Sep11.171215.337@muddcs.claremont.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve From: steve@taumet.com (Steve Clamage) Subject: Re: Integral promotions Message-ID: <1992Sep12.174705.6381@taumet.com> Organization: TauMetric Corporation References: <1992Sep11.171215.337@muddcs.claremont.edu> Date: Sat, 12 Sep 1992 17:47:05 GMT Lines: 45