jeudi 21 mars 2019

C++11 Inject lambda expression into class

The common type-erasure libraries are all C++14 or C++17. As I am stuck to C++11 I thought about writing my own. The problem is that I can't make following code work:

struct Drawable {
  void draw() { poly_call<0>(*this); }

  //How has this macro to look like?
  MAKE_VTABLE([](T const& self) { self.draw(); })
};

//My library-code:
using VTableForT = decltype(Drawable::GetVTable<T>());

To clarify, in C++14 it could look like:

#define MAKE_VTABLE(lambda)         \
    template<class T>               \
    static auto GetVTable() {       \
        return lambda;              \
    }

But auto-return is not yet available in C++11. Any ideas? For me it does not matter whether GetVTable is a static function or a static variable of what ever. I just want to let the user call the macro with his lambda expression and later get its type within my library-code.

Aucun commentaire:

Enregistrer un commentaire