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

标签 c++ metaclass reification c++23 static-reflection

所以我一直很期待metaclasses .然后我听说它不会在 ,因为他们认为我们首先需要在语言中进行反射和具体化,然后才能添加元类。
浏览 反射(reflection),似乎有具体化能力。它们是否足以解决元类会做什么?即,元类只是语法糖吗?
使用 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 值或接口(interface)只是有用的,确切的语法不是),因为我担心它能够做到这一点。

最佳答案

Looking over reflection, there appears to be reification capabilties. Are they sufficient to solve what metaclasses would do; ie, are metaclasses just syntactic sugar?


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

metaclasses are just syntactic sugar on top of the features described [earlier]


正如论文所指出的,元类语法:
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注入(inject)一堆代码的函数。但是为了让它发挥作用,我们需要一个支持 consteval 的语言工具。注入(inject)一堆代码的函数。元类只是在此基础上提供了一个很好的接口(interface),但这只是我们希望我们能够通过代码注入(inject)来做的事情的一部分。

关于c++ - 我们需要元类来做到这一点,还是反射就足够了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67537079/

相关文章:

c++ 为 vector<unsigned char> 查找函数

c++ - 2个类似typedef定义的差异

Python __metaclass__ 继承问题

scala - 什么是 TypeTag 以及如何使用它?

scala - 在 Scala 中重载通用事件处理程序

c# - NUnit:无法加载文件或程序集 'some.dll' 或其依赖项之一。指定的模块无法找到

c++ - 从对象内部返回值给 main

python - 元类和何时/如何调用函数

python - Sphinx autodoc 给出警告 : py:class reference target not found: type warning

c# - C# 泛型如何影响具有原语的集合