Re: ARC and reinterpret_cast

  • >>> From: Rick Mann <rmann...>
    >>> Subject: ARC and reinterpret_cast?
    >>> Date: July 7, 2012 9:13:29 PM PDT
    >>> To: Cocoa-Dev List <cocoa-dev...>
    >>>
    >>> Hi. I'd like to write code like this:
    >>>
    >>> MyObject* foo = reinterpret_cast<__bridge MyObject*> (someVoidPointer);
    >>>
    >>> But the compiler doesn't like it. It's perfectly happy with:
    >>>
    >>> MyObject* foo = (__bridge MyObject) someVoidPointer;
    >>>
    >>> this is in a .mm file.
    >>>
    >>> The error is:
    >>>
    >>> error: type name requires a specifier or qualifier
    >>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>> ^
    >>> error: expected '>'
    >>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>> ^
    >>>>
    >>> note: to match this '<'
    >>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>> ^
    >>> Is it a bug in the compiler, or am I doing something wrong?

    Well, it's definitely an ugly error message;  that's worth a bug.

    The answer is that reinterpret_cast is redundant with __bridge.  Bridging
    casts are essentially a different kind of named cast:  they document intent
    more precisely than the general cast syntax, and they impose their own
    well-formedness rules about the operand and result type.  So you're not
    getting any extra safety here.

    This is documented in the ARC specification:
    http://clang.llvm.org/docs/AutomaticReferenceCounting.html#objects.operands
    .casts

    3.2.4. Bridged casts
      A bridged cast is a C-style cast annotated with . . .

    In theory there's nothing preventing us from allowing these keywords on
    named casts (although it would only be appropriate on reinterpret_cast),
    but our sense is that doing so would only confuse the issue more by
    suggesting subtle differences when none apply.

    John.
  • On Jul 9, 2012, at 16:00 , John McCall wrote:

    >>>> From: Rick Mann <rmann...>
    >>>> Subject: ARC and reinterpret_cast?
    >>>> Date: July 7, 2012 9:13:29 PM PDT
    >>>> To: Cocoa-Dev List <cocoa-dev...>
    >>>>
    >>>> Hi. I'd like to write code like this:
    >>>>
    >>>> MyObject* foo = reinterpret_cast<__bridge MyObject*> (someVoidPointer);
    >>>>
    >>>> But the compiler doesn't like it. It's perfectly happy with:
    >>>>
    >>>> MyObject* foo = (__bridge MyObject) someVoidPointer;
    >>>>
    >>>> this is in a .mm file.
    >>>>
    >>>> The error is:
    >>>>
    >>>> error: type name requires a specifier or qualifier
    >>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>> ^
    >>>> error: expected '>'
    >>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>> ^
    >>>>>
    >>>> note: to match this '<'
    >>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>> ^
    >>>> Is it a bug in the compiler, or am I doing something wrong?
    >
    > Well, it's definitely an ugly error message;  that's worth a bug.
    >
    > The answer is that reinterpret_cast is redundant with __bridge.  Bridging
    > casts are essentially a different kind of named cast:  they document intent
    > more precisely than the general cast syntax, and they impose their own
    > well-formedness rules about the operand and result type.  So you're not
    > getting any extra safety here.
    >
    > This is documented in the ARC specification:
    > http://clang.llvm.org/docs/AutomaticReferenceCounting.html#objects.operands
    .casts

    > 3.2.4. Bridged casts
    > A bridged cast is a C-style cast annotated with . . .
    >
    > In theory there's nothing preventing us from allowing these keywords on
    > named casts (although it would only be appropriate on reinterpret_cast),
    > but our sense is that doing so would only confuse the issue more by
    > suggesting subtle differences when none apply.

    Well, more than anything, I wanted to keep consistent casting in my C++ code. While (__bridge Foo*) might be well-specified, it doesn't look it from the syntax. Maybe add bridge_cast<Foo*>()?

    Thanks for the clarification, in any case.

    --
    Rick
  • Le 18 juil. 2012 à 08:28, Rick Mann <rmann...> a écrit :

    >
    > On Jul 9, 2012, at 16:00 , John McCall wrote:
    >
    >>>>> From: Rick Mann <rmann...>
    >>>>> Subject: ARC and reinterpret_cast?
    >>>>> Date: July 7, 2012 9:13:29 PM PDT
    >>>>> To: Cocoa-Dev List <cocoa-dev...>
    >>>>>
    >>>>> Hi. I'd like to write code like this:
    >>>>>
    >>>>> MyObject* foo = reinterpret_cast<__bridge MyObject*> (someVoidPointer);
    >>>>>
    >>>>> But the compiler doesn't like it. It's perfectly happy with:
    >>>>>
    >>>>> MyObject* foo = (__bridge MyObject) someVoidPointer;
    >>>>>
    >>>>> this is in a .mm file.
    >>>>>
    >>>>> The error is:
    >>>>>
    >>>>> error: type name requires a specifier or qualifier
    >>>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>>> ^
    >>>>> error: expected '>'
    >>>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>>> ^
    >>>>>>
    >>>>> note: to match this '<'
    >>>>> MyObject* me = reinterpret_cast<__bridge MyObject*> (inRefCon);
    >>>>> ^
    >>>>> Is it a bug in the compiler, or am I doing something wrong?
    >>
    >> Well, it's definitely an ugly error message;  that's worth a bug.
    >>
    >> The answer is that reinterpret_cast is redundant with __bridge.  Bridging
    >> casts are essentially a different kind of named cast:  they document intent
    >> more precisely than the general cast syntax, and they impose their own
    >> well-formedness rules about the operand and result type.  So you're not
    >> getting any extra safety here.
    >>
    >> This is documented in the ARC specification:
    >> http://clang.llvm.org/docs/AutomaticReferenceCounting.html#objects.operands
    .casts

    >> 3.2.4. Bridged casts
    >> A bridged cast is a C-style cast annotated with . . .
    >>
    >> In theory there's nothing preventing us from allowing these keywords on
    >> named casts (although it would only be appropriate on reinterpret_cast),
    >> but our sense is that doing so would only confuse the issue more by
    >> suggesting subtle differences when none apply.
    >
    > Well, more than anything, I wanted to keep consistent casting in my C++ code. While (__bridge Foo*) might be well-specified, it doesn't look it from the syntax. Maybe add bridge_cast<Foo*>()?
    >
    > Thanks for the clarification, in any case.
    >

    Just a though, but isn't it possible to define yourself a template function to do that ?

    something like

    template<class C>
    static inline C bridge_cast(void *ptr) { return (__bridge C)ptr;  }

    -- Jean-Daniel
  • On Jul 18, 2012, at 1:22 , Jean-Daniel Dupas wrote:

    > Just a though, but isn't it possible to define yourself a template function to do that ?
    >
    > something like
    >
    > template<class C>
    > static inline C bridge_cast(void *ptr) { return (__bridge C)ptr;  }

    Hey, that's kinda cool! I think that might work.

    --
    Rick
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today