Search code examples
c++objective-cautomatic-ref-countingreinterpret-cast

Using reinterpret_cast with ARC enabled


I have included a header file for a library in my ARC-enabled Objective-C project.

I know the library is not compiled with ARC enabled, but the problem is the header file of the library, specifically these lines:

template <typename Type_>
static inline Type_ &MSHookIvar(id self, const char *name) {
    Ivar ivar(class_getInstanceVariable(object_getClass(self), name));
    void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar));
    return *reinterpret_cast<Type_ *>(pointer);
}

I get this error:

Cast of an Objective-C pointer to 'char *' is disallowed with ARC

Is it possible to fix this error?

The whole header file can be found here: http://puu.sh/sTrH


Solution

  • You need to change the initialization of pointer to this:

    void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>((__bridge void *)self) + ivar_getOffset(ivar));