Path: utzoo!utgpu!cunews!mitel!melair!dataco!amodeo From: amodeo@dataco.UUCP (Roy Amodeo) Newsgroups: comp.lang.c++ Subject: Re: friend operator +(l,r) vs. operator +(r) Message-ID: <305@dcsun21.dataco.UUCP> Date: 30 Nov 90 01:29:54 GMT References: <11759@hubcap.clemson.edu> <1990Nov21.053431.22340@actrix.co.nz> <1990Nov22.230835.26713@clear.com> Reply-To: amodeo@dcsun03.UUCP (Roy Amodeo,DC ) Organization: Canadian Marconi Company (Datacomm), Ottawa, Ontario Lines: 62 In article Reid Ellis writes: >[Personal opinion follows] > > I don't agree with the idea that the result of adding two > objects of different type should return any arbitrary type, as > defined by a global function "operator+(type1, type2)". > Rather, I have a "gut feel" that if I add an orange to an > apple, it is up to the apple to decide what to do with it. I > guess this comes from English where, for example, you add an > element to a set [resulting in a new set] but you don't add a > set to an element.. > > Does anyone else think that > > operator+(const type1 &, const type2 &); > > should have a return type of type1 at all times? > >[end opinion] Not here. If it did, it would imply that the result of var_of_type1 + var_of_type2 is different than var_of_type2 + var_of_type1 and although non-commutative addition is valid in some contexts, the natural view of it is that addition is commutative. Instead, how about this idea: Cast both var_of_type1 and var_of_type2 into type3 which is the minimal type which contains all elements of type1 and type2. For instance, with type1 as apples and type2 as oranges, type3 would be fruit. If type1 was integer and type2 was complex_real, type3 would be complex_real. The addition expression should be performed on two values of type type3 yielding an expression of type3 which can then be cast to type1 or type2 as desired (possibly losing accuracy). This means that addition would always be defined for two operands of the SAME type, requiring (possibly implicit, possibly explicit) casts to coerce operands into the correct type. The result of the operation is always in the supertype again requiring (possibly implicit, possibly explicit) casts to coerce them into the desired resulting type. The downside to this is that if you want to, say, add a scalar to a matrix, you really don't want to convert the scalar to a matrix before performing the addition. In cases like these, you may wish to define addition over heterogeneous types as an optimization, but the semantics of the addition should still behave as if it were the addition of two homogeneous types. Beyond this difficulty, does anyone have any comments on this way of doing things? Is it practical? Is it complete? Is it intuitive? Is there a more "clean" approach? rba iv > > Reid > >-- >Reid Ellis 176 Brookbanks Drive, Toronto ON, M3A 2T5 Canada >rae@gpu.utcs.toronto.edu || rae%alias@csri.toronto.edu >CDA0610@applelink.apple.com || +1 416 446 1644 -- nrcaer!dataco!amodeo Disclaimer: I deny everything.