[comp.std.c++] Should unary operators convert?

krste@ICSI.Berkeley.EDU ( Krste Asanovic) (01/25/91)

Should the unary operators *, &, +, -, !, ~ perform user-defined
conversions on their argument? 

The following example is rejected by g++ (1.37.2b) and cfront
(2.00.02). I've tried replacing the ~ with +, -, ! and get the same
result. I can't find any indication in ARM that the user-defined
conversion wouldn't be used, surely unary operators are treated the
same as the binary/ternary operators?

class A {
  private:
    int a;
  public:
    A(int x) { a = x; };

    operator int() { return a; };
};

main()
{
    A y(3);

    int x;

    x = ~y;
}

%g++ -o bug bug.cc
bug.cc: In function int main ():
bug.cc:18: wrong type argument to bit-complement
%CC -o bug bug.cc
CC  bug.cc:
1 error
cc   -o /tmp_mnt/n/icsib/df/real/krste/tools/g++/library/BitVector/bug -c -I/usr/CC/incl

Bruce.Hoult@bbs.actrix.gen.nz (01/29/91)

Krste Asanovic writes:
>Should the unary operators *, &, +, -, !, ~ perform user-defined
>conversions on their argument? 
> 
>The following example is rejected by g++ (1.37.2b) and cfront
>(2.00.02).


Your example is accepted without complaint by Apple's MPW CFront 1.0  (an
AT&T 2.0 compiler).  The C code produced is also what you would expect.
-- 
Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 772 116
BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ
"And they shall beat their swords into plowshares, for if you hit a man
with a plowshare, he's going to know he's been hit."

Seth_Powsner@QM.YCC.Yale.EDU (Seth M Powsner) (02/02/91)

Seems problematic. Negation - could just as easily convert to float 
(double?) as int. A unary bit operator is perhaps a more interesting case.

Seth M Powsner