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

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

Post by Gil Tay » Thu, 31 Oct 2002 19:18:24



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;

Quote:}

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 ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

 
 
 

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

Post by Garry Lancaste » Fri, 15 Nov 2002 02:20:06


Gil Tayar:

Quote:> 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.

Quote:> 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.

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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

 
 
 

1. type(subtype->supertype)casting causing a wierd (super type) destructor

class Livestock
{
//...
    const string getName() const;
//...

class FatPig: public Livestock
{
//...
    const string getName() const;
//...

int main()
{
//...
FatPig pig1;
//...
//... calling pig1.getName with upcasting(why not try;-))
//... caused one Livestock destruction! WHY?
string pigName = ((Livestock)pig1).getName();

TIA.
wenjie
--
We give nothing so freely as advice

2. Fitting a 3" drive belt

3. static_cast<type>(var) vs. (type)var vs type(var)

4. AMOS Pro help required

5. Class casting in operator<< instead of type casting by pointer

6. can you rank query results?

7. type-cast problem

8. 4 Trade:Cassiopeia A-10 Win CE

9. Overloading type-cast operator ?

10. Templated type-cast operator within templated class problem

11. How To Convert YUV Data From Mpeg-2 Type To Mpeg-1 Type

12. How to obtain referenced type libraries from a given type lib

13. HRESULT return type turns into void when adding class from type library