Topic: Use of type-cast of form type(...)


Author: glancaster@ntlworld.com ("Garry Lancaster")
Date: Wed, 13 Nov 2002 17:20:06 +0000 (UTC)
Raw View
Gil Tayar:
> While writing a tutorial for C++, I compiled the following code (using
> Comeau's online compiler and VC++6):
>
> #include <iostream>
> int main ()
> {
>   const double  pi          = 3.1415926;
>
>   const int     anInt       = int(pi);
>   const int     anotherInt  = (int)pi * 7; // deprecated
>
>   const char* s           = "String theory";
>
>   typedef const unsigned int* UnsignedIntString;
>
>   const unsigned int* s2 = UnsignedIntString(s); // error
>   const unsigned char* s3 = (const unsigned char*)s;   // deprecated
>
>   typedef double* DoublePointer;
>
>   double* ppi = DoublePointer(&pi);   // error
>   int     x   = int(&pi);
>   double* ppi2 = (double*)&pi;     // deprecated
>
>   std::cout << pi << anInt << s << s2 << s3 << ppi << x << ppi2 <<
> anotherInt;
> }
>
>
> To my surprise, the lines marked by "error" were not errors! I checked
> the standard, and yes - they are not errors (Std, section 5.4
> paragraph 5).

And the old-style casts you commented as "deprecated"
are not deprecated either.

> Why did I think they were errors? Because I remembered that the
> "type(...)" form of casting was supposed to guard you against casting
> away the const or doing a reinterpret of a pointer to an int or a
> pointer of one type to a pointer of another type. This was supposed to
> deprecate (type)... which didn't guard you aginst those "unsafe"
> conversions.
>
> Seems I was wrong. type(...) and (type)... are identical. I have two
> questions:
>
> 1. Why did I remember that "type(...)" is safe?

Can't answer that one.

> 2. If I want to do a "safe cast" (one which only casts numeric/bool
> types and does not cast away constness or others), do I have an
> option?

Sounds like static_cast<> would be the best fit of the
new-style casts, although it is not a perfect fit. It does
the numeric/bool casting and does not cast away
constness, but it also does unchecked base to derived
conversions.

Whether any cast is a "safe cast" depends on context
and your definition of "safe". Still, I think it's reasonable
to say that static_cast<>is safer than either the
functional- or old-style casts.

Kind regards

Garry Lancaster
Codemill Ltd
Visit our web site at http://www.codemill.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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gil@tayar.org (Gil Tayar)
Date: Wed, 30 Oct 2002 10:18:24 +0000 (UTC)
Raw View
While writing a tutorial for C++, I compiled the following code (using
Comeau's online compiler and VC++6):

#include <iostream>
int main ()
{
  const double  pi          = 3.1415926;

  const int     anInt       = int(pi);
  const int     anotherInt  = (int)pi * 7; // deprecated

  const char* s           = "String theory";

  typedef const unsigned int* UnsignedIntString;

  const unsigned int* s2 = UnsignedIntString(s); // error
  const unsigned char* s3 = (const unsigned char*)s;   // deprecated

  typedef double* DoublePointer;

  double* ppi = DoublePointer(&pi);   // error
  int     x   = int(&pi);
  double* ppi2 = (double*)&pi;     // deprecated

  std::cout << pi << anInt << s << s2 << s3 << ppi << x << ppi2 <<
anotherInt;
}


To my surprise, the lines marked by "error" were not errors! I checked
the standard, and yes - they are not errors (Std, section 5.4
paragraph 5).

Why did I think they were errors? Because I remembered that the
"type(...)" form of casting was supposed to guard you against casting
away the const or doing a reinterpret of a pointer to an int or a
pointer of one type to a pointer of another type. This was supposed to
deprecate (type)... which didn't guard you aginst those "unsafe"
conversions.

Seems I was wrong. type(...) and (type)... are identical. I have two
questions:

1. Why did I remember that "type(...)" is safe?
2. If I want to do a "safe cast" (one which only casts numeric/bool
types and does not cast away constness or others), do I have an
option?

Thanks,
Gil

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]