[cfe-dev] Specializing templates on Objective-C ARC ownership annotations

John McCall via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 31 15:51:27 PDT 2018


> On Jul 31, 2018, at 6:14 PM, Louis Dionne via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> I'm trying to partially specialize a template on ARC-annotated pointer types, and I'm finding it doesn't work. Specifically, the following fails because the "specialization" for `T __strong*` is never considered, and we always fall back to the specialization for `T*`:
> 
>    // clang test.mm -fobjc-arc -ObjC++ -std=c++14
> 
>    #import <Foundation/NSObject.h>
> 
>    @interface Foo: NSObject
>    { }
>    @end
> 
>    template <typename T> struct Specialize;
>    template <typename T> struct Specialize<T*> { static constexpr bool value = false; };
>    template <typename T> struct Specialize<T __strong*> { static constexpr bool value = true; };
>    static_assert(Specialize<Foo __strong*>::value, "");
> 
>    int main() { }
> 
> I have a few questions:
> 
> - Does this even make sense? I don't know much of Objective-C, so it's possible that treating ownership qualifiers like cv qualifiers is not even sensical.
> - If it does make sense, why is it not supported? Is this simply a bug that needs fixing?

Somewhat confusingly (for this sort of purpose), the ARC ownership qualifiers magically move to the right place in a declarator when parsed.  The qualifier applies to Objective-C object pointer types, and `Foo` is a bare object type, so `Foo __strong *` actually means `Foo * __strong`.  This doesn't (and couldn't) happen for an arbitrary template parameter, so `T __strong *` will never match that; it would only match something like `Foo * __strong *`.

John.


> 
> Strangely enough, I haven't found anything online regarding this, so I'm turning to this list as a last resort.
> 
> Thanks,
> Louis
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list