我们需要元类来做到这一点,还是反射就足够了?

如何解决我们需要元类来做到这一点,还是反射就足够了?

所以我一直很期待metaclasses。然后我听说它不会出现在 中,因为他们认为我们首先需要在语言中进行反思和具体化,然后才能添加元类

查看 反射,似乎有具体化功能。它们是否足以解决元类会做什么?即,元类只是语法糖吗?

使用 current proposal,我们可以复制某人编写如下类型:

interface bob {
  void eat_apple();
};

生成如下类型:

struct bob {
  virtual void eat_apple() = 0;
  virtual ~bob() = default;
};

更进一步,采取类似于

vtable bob {
  void eat_apple();
  ~bob();
};
poly_value bob_value:bob {};

并且能够生成

// This part is optional,but here we are adding
// a ADL helper outside the class.
template<class T>
void eat_apple(T* t) {
  t->eat_apple();
}

struct bob_vtable {
  // for each method in the prototype,make
  // a function pointer that also takes a void ptr:
  void(*method_eat_apple)(void*) = 0;

  // no method_ to guarantee lack of name collision with
  // a prototype method called destroy:
  void(*destroy)(void*) = 0;

  template<class T>
  static constexpr bob_vtable create() {
    return {
      [](void* pbob) {
        eat_apple( static_cast<T*>(pbob) );
      },[](void* pbob) {
        delete static_cast<T*>(pbob);
      }
    };
  }
  template<class T>
  static bob_vtable const* get() {
    static constexpr auto vtable = create<T>();
    return &vtable;
  }
};
struct bob_value {
  // these should probably be private
  bob_vtable const* vtable = 0;
  void* pvoid = 0;

  // type erase create the object
  template<class T> requires (!std::is_base_of_v< bob_value,std::decay_t<T> >)
  bob_value( T&& t ):
    vtable( bob_vtable::get<std::decay_t<T>>() ),pvoid( static_cast<void*>(new std::decay_t<T>(std::forward<T>(t))) )
  {}
  
  ~bob_value() {
    if (vtable) vtable->destroy(pvoid);
  }

  // expose the prototype's signature,dispatch to manual vtable
  // (do this for each method in the prototype)
  void eat_apple() {
    vtable->method_eat_apple(pvoid);
  }

  // the prototype doesn't have copy/move,so delete it
  bob_value& operator=(bob_value const&)=delete;
  bob_value(bob_value const&)=delete;
};

Live example,这两个例子都是我对元类感到兴奋的例子。

我不太担心语法(能够编写库并创建 poly 值或接口只是有用的,确切的语法不是),因为我担心它能够做到这一点。>

解决方法

查看 反射,似乎有具体化功能。它们是否足以解决元类会做什么?即,元类只是语法糖吗?

将其称为 C++23 反射是……乐观的。但答案是肯定的。引用P2237

元类只是[前面]描述的功能之上的语法糖

正如论文所指出的,元类语法:

template<typename T,typename U>
struct(regular) pair{
    T first;
    U second;
};

意味着:

namespace __hidden {
    template<typename T,typename U>
    struct pair {
        T first;
        U second;
    };
}

template <typename T,typename U>
struct pair {
    T first;
    U second;

    consteval {
        regular(reflexpr(pair),reflexpr(__hidden::pair<T,U>));
    }
};

其中 regular 是一些注入一堆代码的 consteval 函数。但是为了让它完全发挥作用,我们需要有一种语言工具来支持 consteval 函数,该函数可以注入一堆代码。元类只是在此基础上提供了一个很好的接口,但这只是我们希望通过代码注入能够做的事情的一部分。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?