Topic: Conversion to self - legal?


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/05/13
Raw View
phalpern@truffle.ultranet.com (Pablo Halpern) writes:

>Is the following definition legal according to the current DWP?

>class x {
>public:
>  x();
>  operator x() const;  // LINE A: conversion from x to x
>};

>This may seem like a perverse thing to do, but it comes up (as many
>perverse things do) in the context of templates:

>template <class T>
>class Q { public: operator Q<const int>() const; };

>If T is const int, then the conversion operator becomes a conversion of
>a class to itself.

The draft standard says you can provide a conversion to self, but
it will not be used to perform a conversion. (It might get called
via a virtual conversion operator in a base class, but not otherwise.)
See section 12.3.2 "Conversion functions" [class.conv.fct].

I believe the reason for the rule is your example: strange things
can happen as a result of instantiating templates.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: jason@cygnus.com (Jason Merrill)
Date: 1996/05/13
Raw View
>>>>> Pablo Halpern <phalpern@truffle.ultranet.com> writes:

> My questions for the language lawyers, therefore, are as follows:

>   Is it valid to define an operator to convert x to x?

Yes.

>   Is upcasting from a derived class to a base class considered a
>     conversion, equal in precidence to user-defined conversions?

No; it is a standard conversion, of better rank than a user-defined
conversion.

>   Can user-defined conversions ever override built-in conversions (e.g.
>     from x to const x, from derived to base, from x to x&, etc.)?

No.  The conversion operators can be defined, but will not override builtin
conversions.

  12.3.2  Conversion functions                          [class.conv.fct]

            A conversion oper-
  ator is never used to convert a (possibly cv-qualified) object to  the
  (possibly  cv-qualified) same object type (or a reference to it), to a
  (possibly cv-qualified) base class of that type  (or  a  reference  to
  it), or to (possibly cv-qualified) void.
  _________________________
  1)  Even  though  never  directly called to perform a conversion, such
  conversion operators can be declared and can  potentially  be  reached
  through a call to a virtual conversion operator in a base class

Jason
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: phalpern@truffle.ultranet.com (Pablo Halpern)
Date: 1996/05/10
Raw View
Is the following definition legal according to the current DWP?

class x {
public:
  x();
  operator x() const;  // LINE A: conversion from x to x
};

This may seem like a perverse thing to do, but it comes up (as many
perverse things do) in the context of templates:

template <class T>
class Q { public: operator Q<const int>() const; };

If T is const int, then the conversion operator becomes a conversion of
a class to itself. My compiler (Borland 4.02) doesn't mind this and the
code works as intended (converting any Q<T> to a Q<const int>). I think
this is good, so I hope it is, in fact legal according to the DWP. What
do the language lawyers think? Theoretically, x::operator x() would
never be used, but I have had the compiler complain about it in the
presence of inheritence:

class R : public Q<const int> { };

R r;
Q<const int> q(r); // upcast or call "r.operator Q<const int>()" ?

My questions for the language lawyers, therefore, are as follows:

  Is it valid to define an operator to convert x to x?
  Is upcasting from a derived class to a base class considered a
    conversion, equal in precidence to user-defined conversions?
  Can user-defined conversions ever override built-in conversions (e.g.
    from x to const x, from derived to base, from x to x&, etc.)?

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent
those of my employer.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]