Re: static_cast in squid3

From: Robert Collins <robertc@dont-contact.us>
Date: 09 Nov 2002 09:20:18 +1100

On Sat, 2002-11-09 at 02:50, Evgeny Kotsuba wrote:
> Hi,
> On 08 Nov 2002 21:57:30 +1100
> Robert Collins <robertc@squid-cache.org> wrote:
> >> Why use static_cast in squid3 ???
> [...]
> >> next it is
> >> not supported on some compilers
> >
> >What compilers don't support it?
> IBM VAC++ 3.08, gcc prior to 3.0

gcc 2.95 supports it. Can you be more specific about the gcc version
that doesn't? And is there a newer port of gcc for your OS?

Hmm, IBM are usually quite good, is 3.08 there newest one? static_cast
is a very simple C++ construct, it's suprising it's not supported.

> >> and last it is longer then pure
> >> typecast.
> >
> >static_cast ensures that the typecast is safe, AFAIK a C-style cast
> >doesn't check all the operators.
> ??????
> Isn't the static_cast synonym to the type cast ?

No.

> Isn't the C-style cast one of two styles of C++'s cast exression with
> the same functionality ?

There are many C++ casts:
static_cast
dynamic_cast
const_cast
reinterpret_cast

the 'C cast' is something like (int *)foo. It's supported in C++, but we
shouldn't use it.

static cast requires a typesafe static conversion. It calculates the
typecast at compile time. It's very close to a C cast, but not
identical. C casts allow *any* cast to occur, static casts require
appropriate operators.

const cast can not change from one class to another, only remove or add
the 'const' flag from a variable.

dynamic casts require run time type information to work, and allow you
to safely cast from one class to another in a hierarchy, both up and
down the hierarchy. If the cast fails, you get a NULL pointer/reference.

reinterpret_cast is able to change pointers from any type to any other
type - very unsafe.

> >Also, note that static casts will be unneeded once we've fully
> >converted
> >the code to good C++ design.
> I hope for that, but it is ipossible, as minimum with sorting
> functions.

It's trivial to write sorting functions without static casts ( I haven't
actually built this, there may be minor mistakes in my typing):

#include <iostreams>

class Foo {
public:
  Foo (int seed):aMember(seed){}
  bool operator < (Foo const &rightHandSide) {return aMember <
rightHandSide.aMember);}
private:
  int aMember;
};

int
main (int argc, char ** argv)
{
   Foo a(50), b(60);
   if (a < b)
      cout << "A < B" << endl;
   else
      cout << "A >= B" << endl;
}

Rob

Received on Fri Nov 08 2002 - 15:20:21 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:18:39 MST