🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Jason Merrill Fri, 11 Sep 1992 17:12
Integral promotions
#145
Author: Jason Merrill
Date: Fri, 11 Sep 1992 17:12
14 lines
804 bytes
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.

--
Jason Merrill
jason@jarthur.claremont.edu
Re: Integral promotions
#152
Author: Steve Clamage
Date: Sat, 12 Sep 1992 17:47
45 lines
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads