[cfe-dev] GCC-specific, Clang-specific and ObjC-specific casts in C++

David Chisnall csdavec at swansea.ac.uk
Tue Jul 28 11:44:44 PDT 2009


On 28 Jul 2009, at 19:08, Sebastian Redl wrote:

> I expect that you can convert pointers to related interfaces
> (protocols?) and classes in Objective-C as well, and I wonder how
> Objective-C++ should treat attempts to cast Obj-C types via C++ cast
> operators.

Protocols in Objective-C are just objects (instances of the Protocol  
class) so they don't need any special handling.

There is no multiple inheritance in Objective-C, so any related issues  
are not present.

Casting a derived class to a superclass can happen implicitly, as can  
casting to id (or Class if it's a class).

The GCC behaviour causes an error if you attempt to use static_cast to  
cast from a pointer to one class to a pointer to an unrelated class.   
This seems sensible.

The GCC behaviour allows you to use static_cast to cast from a pointer  
to one class to a pointer to a subclass.  This seems sensible.

The GCC behaviour allows you to use reinterpret_cast to cast from  
anything (including int) to an Objective-C pointer type.  This seems  
to be equivalent to a C-style cast (which is also permitted).

There appears to be a bug in GCC's Objective-C++ handling[1].  If you  
pass a pointer to an object to a function that takes a subclass as an  
argument, then GCC silently performs an implicit cast in Objective-C++  
mode issues a warning in Objective-C mode.  This may simply be due to  
different default warnings.  This is probably not behaviour that we  
want to copy.

To be honest, I doubt this is very important.  I'd be surprised if  
anyone is relying on specific behaviour of this corner case; Objective- 
C++ is mostly used for Objective-C wrappers around C++ libraries (and  
is basically unmaintained in the GCC tree, so no one with any sense is  
using it anywhere other than OS X).

David

[1] i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5484)



More information about the cfe-dev mailing list