Topic: Base class conversion - former section 4.1


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/17
Raw View

Steve Clamage writes:

> Example: Initializing a Base object with a Dervied rvalue
>  class Base { ... };
>  class Derived : public Base { ... };
>  Derived d;
>  Base b = d;

> The last line does not involve an rvalue conversion. It is equivalent to
>  Base b(Base(d));

AFAIK, the line above declares a function named b that takes a Base
argument (named d) and returns an object of class Base.  It should
have been said to be equivalent to:

        Base b = Base(d);

unless Base provided a constructor that took a reference to a Derived
(but who'd do that anyway? :-)

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1997/04/11
Raw View
In article A74A8689@ucla.edu, Max Moroz <mmoroz@ucla.edu> writes:
>In the draft standard of mid-1996 there was a clause 4.12 about base
>class conversion being one of the standard conversions. In December 1996
>draft standard, this clause disappeared.
>
>What is the reason for that?

It turns out no standard conversion from a Derived rvalue to a Base
rvalue is ever needed, except in some rather obscure cases. The
obvious cases of assigning a Derived object to a Base object or
passing a Derived object by value to a function taking a Base object
involve copy constuctors and reference conversion, not rvalue conversion.

Example: Initializing a Base object with a Dervied rvalue
 class Base { ... };
 class Derived : public Base { ... };
 Derived d;
 Base b = d;

The last line does not involve an rvalue conversion. It is equivalent to
 Base b(Base(d));
which in turn involves a conversion from Derived to Derived& followed
by Derived& to Base&, and two calls of the copy constructor --
Base::Base(const Base&) -- one of which can be optimized away. A function
call passing a Dervied by value to a Base parameter has exactly the same
interpretation. Assignment of a Derived rvalue to a Base rvalue means
creating a temp Base rvalue and assigning that -- no rvalue conversions.

In addition, making Derived-to-Base rvalue a standard conversion leads to
some complexities in interpreting other rules. For example, it seems
to interact with rules about implicit user-defined conversions.

The C++ Committee removed the Derived-to-Base rvalue conversion from the
list of standard conversions, and modified the the overload resolution
rules in 13.3.3.1 "Implicit conversion sequences" to cover the obscure
cases which would otherwise have been affected. The result is no change
in the legality of programs, but a more consistent treatment of conversion
rules.

The subject is discussed in detail in a 9-page paper submitted to
the C++ Committee by Steve Adamczyk: WG21/N0839, "Eliminating the Class
Rvalue Standard Conversion", Jan 18, 1996. I'll email a postscript
or PDF version to anyone who wants a copy.

---
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
]