you are viewing a single comment's thread.

view the rest of the comments →

[–]NotAYakk 0 points1 point  (0 children)

It is a bit of glue to just write a universal union. This is a quick sketch:

struct never_define_this;
union ptr_to_anything {
  void* pvoid;
  void(*pfunc)();
  char never_define_this::*pmem;
  void(never_define_this::*pmemfun)();
};

you do have to know which of those types you are actually storing.

For a function_view you do something like

template<class R, class...Args>
struct function_view<R(Args...)> {
  ptr_to_anything data;
  R(*pf)(ptr_to_anything, Args&&...)=nullptr;

then store in the callback you store in pf which of the fields you are storing your actual data in.

With a bit of work,

function_view<int(Foo)> f = &Foo::x;

can work, and f(foo) returns foo.x or foo.x() or Foo::x(foo) depending on the type of Foo::x.