Topic: Making a list of unnamed casts


Author: dwalker07@snet.net.invalid (Daryle Walker)
Date: 2000/11/15
Raw View
I started two threads about two uses of the C-style cast that are not
covered (AFAIK) by any of the C++-style casts.

1. A "distinguish_cast" to choose a particular function overload,
especially when the function name is being used as a pointer, so we
don't get argument cues.  (Technically, this isn't really a cast at all,
but I don't know how a brand-new construct would be received.)
2. A "violating_cast" to get access at a private or protected part of a
class (usually a base class) that the current function can't access.

If there any other uses of the C-style cast that can't be explained by a
C++-style cast, let's list them here and eventually get explicit
versions of them added to the next standard.

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/11/15
Raw View
Daryle Walker <dwalker07@snet.net.invalid> wrote:

> I started two threads about two uses of the C-style cast that are not
> covered (AFAIK) by any of the C++-style casts.
>=20
> 1. A "distinguish_cast" to choose a particular function overload,
> especially when the function name is being used as a pointer, so we
> don't get argument cues.  (Technically, this isn't really a cast at all=
,
> but I don't know how a brand-new construct would be received.)

If I understand you correctly, this is covered by static_cast. Basically
static_cast can do anything an implicit conversion can do:

  void f(long, int);
  char f(int, long);

  char c =3D static_cast<char (&)(int,long)>(f)(1,1);

Regards, J=F6rg
--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: celtschk@Physik.TU-Muenchen.DE (Christopher Eltschka)
Date: 2000/11/16
Raw View
dwalker07@snet.net.invalid (Daryle Walker) writes:

>I started two threads about two uses of the C-style cast that are not
>covered (AFAIK) by any of the C++-style casts.

>1. A "distinguish_cast" to choose a particular function overload,
>especially when the function name is being used as a pointer, so we
>don't get argument cues.  (Technically, this isn't really a cast at all,
>but I don't know how a brand-new construct would be received.)

This is a special case of an "implicit_cast", which is easily
done in the language:

template<class T>
 inline T implicit_cast(T t) { return t; }

This does only conversions which can be done implicitly anyway
(after all, the construct itself doesn't make any conversion,
but the conversion is done at the caller's site), therefore it's safe.
Selecting a function could then be done with

void foo(int);
void foo(char);

do_something_with(implicit_cast<void(*)(int)>(foo));
// selects void foo(int)

BTW, this is _not_ something which the new style casts cannot do.
A static_cast works quite well (but is of course less safe than the
implicit_cast version above - but still safer than a C style cast).

>2. A "violating_cast" to get access at a private or protected part of a
>class (usually a base class) that the current function can't access.

>If there any other uses of the C-style cast that can't be explained by a
>C++-style cast, let's list them here and eventually get explicit
>versions of them added to the next standard.

from CD2, 5.4  [expr.cast]:

7 In  addition to those conversions, the following static_cast and rein-
  terpret_cast operations (optionally followed by  a  const_cast  opera-
  tion)  may  be performed using the cast notation of explicit type con-
  version, even if the base class type is not accessible:

  --a pointer to an object of derived class type or an lvalue of derived
    class  type may be explicitly converted to a pointer or reference to
    an unambiguous base class type, respectively;

  --a pointer to member of derived class type  may  be  explicitly  con-
    verted  to  a  pointer  to member of an unambiguous non-virtual base
    class type;

  --a pointer to an object of non-virtual base class type, an lvalue  of
    non-virtual  base  class type, or a pointer to member of non-virtual
    base class type may be explicitly converted to a pointer,  a  refer-
    ence,  or a pointer to member of a derived class type, respectively.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]