删除指针属性

问题描述

我有

template<typename T>
struct is_objective_c_type {
private:
  // Removes all pointer indirection.
  // object*** => object
  // object**  => object
  // object*   => object

  template<class U> struct remove_pointer                    {typedef U type;};
  template<class U> struct remove_pointer<U*>                {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* const>          {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* volatile>       {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* const volatile> {typedef typename remove_pointer<U>::type type;};
  
public:
  static const bool value = std::is_base_of<NSObject,typename remove_pointer<T>::type>::value;
};

目前它的工作原理是:

@interface Foo: NSObject
@end

@implementation Foo
@end

is_objective_c_type<Foo>;  // true
is_objective_c_type<Foo*>;  // true
is_objective_c_type<Foo* __weak>; // false
is_objective_c_type<Foo* const*>;  // true

如您所见,如果我添加 __weak 属性,它会失败。 但是,如果我将 remove_pointer 的模板更改为:

template<class U> 
struct remove_pointer<U* __weak>
{
    typedef typename remove_pointer<U>::type type;
};

它会起作用的。但这意味着它永远不会对 Foo* 起作用,因为它是隐含的 __unsafe_unretained 并且它也不会对 Foo* __strong 起作用。

目前,如果我定义多个,它会给我 ambiguous specialization

template<class U> 
struct remove_pointer<U* __weak> { .. };

template<class U> 
struct remove_pointer<U* __unsafe_unretained> { .. };

template<class U> 
struct remove_pointer<U* __strong> { .. };

有没有办法删除 ownership 或专门化模板以处理不同的所有权?

解决方法

我最终做了:

template<typename T>
struct is_objective_c_type {
private:
  template<class U> struct remove_pointer                    {typedef U type;};
  template<class U> struct remove_pointer<U*>                {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* const>          {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* volatile>       {typedef typename remove_pointer<U>::type type;};
  template<class U> struct remove_pointer<U* const volatile> {typedef typename remove_pointer<U>::type type;};
  
public:
  static const bool value = std::is_base_of<NSObject,typename remove_pointer<T>::type>::value ||
                            std::is_convertible<typename remove_pointer<T>::type,NSObject*>::value;
};

因为它似乎是唯一适用于 __weak__strong__unsafe_unretained 等的东西。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...