lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 28 Jun 2015, at 14:05, Ole Krüger <ole@vprsm.de> wrote:

That is only possible to do with a macro, because templates can only match against typenames/classes. Matching against the function or method signature is necessary in order to generate an appropriate Lua C function.


That’s not correct - you can template and specialize on values. What you can’t do is have a value whose type depends on another templated type, but an inner class can get around this. There is an alternative but non-standard way i’ve used in the past, but if I had to do this kind of integration with Lua again I’d rather have C style functions with an explicit self.

Hopefully this demonstrates - but please don’t mistake for useful or pretty code :)

#include <string>
#include <iostream>

struct C { double b (std::string a) { return 2.5; } };

template<typename C, typename G, typename F> struct Wrapper {
public:
    typedef G (C::*M) (F);
    template<M m> struct Inner { G operator() (C& r, F f) { return ((&r)->*m) (f); } };
};

Wrapper<C, double, std::string>::Inner<&C::b> w;


double res = w (c, "A”);