static_cast

R

Rahul

Hi,

Everywhere I read that static_cast<> only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK

But surprisingly the following static_cast from Base * to Derived *
works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)
 
K

Kai-Uwe Bux

Rahul said:
Hi,

Everywhere I read that static_cast<> only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK

Ahem, no: the mentioned reason is not recognized by the standard.

The reason that the above does not work is that this conversion is not
mentioned in [5.2.9].

But surprisingly the following static_cast from Base * to Derived *
works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)

See [5.2.9/8].

However, you can only use this safely (i.e., without undefined behavior) if
you _know_ for other reasons that the pointer to base _is_ indeed a pointer
to derived. If you cannot be sure, you better use dynamic_cast<> and test
for 0.


Best

Kai-Uwe Bux
 
J

Juha Nieminen

Kai-Uwe Bux said:
But surprisingly the following static_cast from Base * to Derived *
works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)

See [5.2.9/8].

Btw, another situation where implicit cast fails and static_cast is
valid is when casting a void* to some other type of pointer.
 
J

James Kanze

Everywhere I read that static_cast<> only work fine for the
conversion which are implicitly allowed by the compiler

Where's everywhere. I've never read that.

As a very rough first approximation, static_cast can be used
where ever the converision or its inverse would be implicitly
allowed, and const-ness is preserved.
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK

Since the two pointers are to unrelated types, there is no
conversion in either direction, and the static cast is
forbidden. static_cast< void* >( d ) is legal, however (since
there is an implicite conversion double* to void), as if
But surprisingly the following static_cast from Base * to
Derived * works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)

Because there is an implicit conversion Derived* to Base*, and
you're not removing const. (Also because this particular case
is spelled out explicitly in the standard. In fact, all of the
cases are more or less spelled out explicitly, since it isn't
sufficient for the standard to say that the cast is legal; it
also has to specify its semantics.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top