Topic: functional cast must invoke copy ctor?


Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@germany.sun.com>
Date: 2000/01/25
Raw View

>>>>>>>>>>>>>>>>>> Urspr   ngliche Nachricht <<<<<<<<<<<<<<<<<<

Am 22.01.00, 17:04:03, schrieb Sandra Loosemore <sandra@shore.net> zum
Thema Re: functional cast must invoke copy ctor?:


> jbarfurth@vossnet.de (Joerg Barfurth) writes:
> > 5.4 enumerates the possible explicit casts. With X a class type
5.4/p.5
> > enumerates all possibilities.
> >
> > const_cast applies to pointer and reference types only,

> This is where I'm getting confused.  Doesn't const_cast explicitly
> permit casting an expression to its own type, in addition to the
> conversions on pointer and reference types?

> -Sandra

It does explicitly permit casting an expression to its own type, but
not    in addition   . If the type and the expression otherwise qualify as
target or operand of a const_cast it is allowed that the target type
is the same as the type of the expression. This means that a
const_cast doesn't have to add or remove constness. The paragraph you
refer to does not enlarge the range of types eligible as the target of
a const_cast.
Example:
 int n = 0;
 int * p = &n;
 int ** pp = &p;

 int** qq = const_cast<int**>(pp); // OK
 int * q  = const_cast<int*>(p); // OK
 int m = const_cast<int>(n); // ERROR

--
J   rg Barfurth
+jbarfurth@vossnet.de
+joerg.barfurth@germany.sun.com
http://www.sun.com/staroffice








---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Eric Nagler <epn@eric-nagler.com>
Date: 2000/01/22
Raw View
On 14 Jan 2000 17:39:18 GMT, Sandra Loosemore <sandra@shore.net>
wrote:
>I've run across some code that seems to expect that an expression
>of the form X(x), where x is an expression of class type X, will invoke
>X's copy constructor.

Note this interesting example:

#include <iostream>
using namespace std;
class ADT
{
    public:
  ADT() {}
  ADT(ADT const &)
  {
    cout << "copy ctor\n";
  }
};
int main()
{
  ADT a;
  ADT(a); // Error; this statement tried to redeclare 'a'
  ADT b = ADT(a); // OK; calls copy ctor once for BCB4, twice for VC++
}

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Sandra Loosemore <sandra@shore.net>
Date: 2000/01/22
Raw View
jbarfurth@vossnet.de (Joerg Barfurth) writes:

> Let's see.
>     X is a class type
>     x is variable of type X
>     We consider the expression X(x)
>
> 5.2.3 says that this expression (function-style cast) is equivalent to
> (and subject to the same restrictions as) an old style cast X(x).
>
> 5.4 enumerates the possible explicit casts. With X a class type 5.4/p.5
> enumerates all possibilities.
>
> const_cast applies to pointer and reference types only,

This is where I'm getting confused.  Doesn't const_cast explicitly
permit casting an expression to its own type, in addition to the
conversions on pointer and reference types?

-Sandra

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/01/23
Raw View
Sandra Loosemore wrote:
>
> jbarfurth@vossnet.de (Joerg Barfurth) writes:
>
> > Let's see.
> >     X is a class type
> >     x is variable of type X
> >     We consider the expression X(x)
> >
> > 5.2.3 says that this expression (function-style cast) is equivalent to
> > (and subject to the same restrictions as) an old style cast X(x).
> >
> > 5.4 enumerates the possible explicit casts. With X a class type 5.4/p.5
> > enumerates all possibilities.
> >
> > const_cast applies to pointer and reference types only,
>
> This is where I'm getting confused.  Doesn't const_cast explicitly
> permit casting an expression to its own type, in addition to the
> conversions on pointer and reference types?

No.

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Eric Nagler <epn@eric-nagler.com>
Date: 2000/01/23
Raw View
On Sat, 22 Jan 2000 16:04:03 CST, Sandra Loosemore <sandra@shore.net>
wrote:

>This is where I'm getting confused.  Doesn't const_cast explicitly
>permit casting an expression to its own type, in addition to the
>conversions on pointer and reference types?

const_cast allows you to either add or eliminate the "constantness" of
an object. That's all.

char const *const_ptr = "C++";
char *non_const_ptr = const_cast<char *>(const_ptr);
// or...
char *non_const_ptr = "C++";
char const *const_ptr = const_cast<char const *>(non_const_ptr);


EriC++

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Sandra Loosemore <sandra@shore.net>
Date: 2000/01/14
Raw View
I tried posting this question last week but it never showed up, so
here's another try....

[ moderator's note: Please refer to the FAQ for a discussion of what
  to do when your message does not appear in the newsgroup. -sdc ]

I've run across some code that seems to expect that an expression
of the form X(x), where x is an expression of class type X, will invoke
X's copy constructor.

Section 5.2.3 of the standard says that the one-argument version of the
function type conversion notation is exactly equivalent to an old-style
C cast, e.g. (X)x.  And I don't see anything in the discussion of casts
that would require an identity cast operation to invoke a copy constructor.

Section 12.1 says that explicit type conversion using functional
notation will cause a constructor to be called.  However, is this supposed
to apply only to the 0-argument and multi-argument cases, where it is
already clear from 5.2.3 that a constructor is called, or is it supposed
to also override the language in 5.2.3 that describes the treatment of
the one-argument version?

-Sandra


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jbarfurth@vossnet.de (Joerg Barfurth)
Date: 2000/01/15
Raw View
Sandra Loosemore <sandra@shore.net> wrote:

> I tried posting this question last week but it never showed up, so
> here's another try....
>
> [ moderator's note: Please refer to the FAQ for a discussion of what
>   to do when your message does not appear in the newsgroup. -sdc ]
>
> I've run across some code that seems to expect that an expression
> of the form X(x), where x is an expression of class type X, will invoke
> X's copy constructor.

Useful example:
    vector<int> v(1,42);
    v.reserve(10000);
    // get rid of extra storage in v:
    vector<int>(v).swap(v);

> Section 5.2.3 of the standard says that the one-argument version of the
> function type conversion notation is exactly equivalent to an old-style
> C cast, e.g. (X)x.  And I don't see anything in the discussion of casts
> that would require an identity cast operation to invoke a copy constructor.

Let's see.
    X is a class type
    x is variable of type X
    We consider the expression X(x)

5.2.3 says that this expression (function-style cast) is equivalent to
(and subject to the same restrictions as) an old style cast X(x).

5.4 enumerates the possible explicit casts. With X a class type 5.4/p.5
enumerates all possibilities.

const_cast applies to pointer and reference types only,
reinterpret_cast doesn't apply to class types,
thus we need a static_cast
Note: A plain implicit conversion (e.g. lvalue to rvalue or empty) is
not listed.

5.2.9 is our final stop

Paragraph 1 says that the result must be an rvalue.

Paragraph 2 covers the conversion we are looking for. Effecitively it
says for our case, that if a temporary X can be constructed from x [*],
then the effect is to construct the temporary and use it as the
(rvalue-) result of the cast.

Paragraph 3 restricts what can be done if such a temporary can't be
constructed. But the remainder of 5.2.9 doesn't offer anything in our
case. [**]
Note: lvalue to rvalue or empty conversions are not listed.

RESULT: The cast will invoke the copy constructor of X, if present.
Otherwise the cast is ill-formed.

Footnote:
[*] Generally this requires a copy constructor. std::auto_ptr shows that
there may be exceptions to this rule.
[**] Unless an empty implicit conversion sequence is also an empty
standard conversion. But in this case the cast would only work (P.6 -
without copy) if x is an rvalue and not if it is an lvalue. This strange
interpretation was surely not intended.

> Section 12.1 says that explicit type conversion using functional
> notation will cause a constructor to be called.  However, is this supposed
> to apply only to the 0-argument and multi-argument cases, where it is
> already clear from 5.2.3 that a constructor is called, or is it supposed
> to also override the language in 5.2.3 that describes the treatment of
> the one-argument version?

As I demonstrated, there is nothing to override.

J   rg

--
J   rg Barfurth                   Tel.: +49 40 23646 500
Software Engineer               mailto:joerg.barfurth@germany.sun.com
+jbarfurth @ vossnet.de             http://www.sun.com/staroffice



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]