-1

In working with a framework (Godot) that uses a register_method(<name>, <pointer_to_method>) to register a c++ method to a scripting API. this method however doesn't support pointer to template classes.

So in the example:


static void _register_methods() {
    register_method("my_method", &TMyClass::my_method); // this fails
                               // ^ template class

    register_method("my_method", &MyClass::my_method); // this works
                               // ^ normal class
}

I have a template class TExample and an Example that extends the template class. The methods declarations and method definitions are all inside the TExample (however the methods are registered in Example).

So when I do:

register_method("my_method", &Example::my_method); // this fails because it is referencing the method of the parent class (template).

What I've found that works is redirecting the methods to "local" methods.

class Example : TExample<...>
{
    public:
        void my_method() {
            TExample::my_method();
        }

        static void _register_methods() {
            register_method("my_method", &Example::my_method); // this works
        }
}

But imagine I have like 50 methods every time I want to create a new class from the template I need to redirect 50 methods. is there a shortcut to do this?!

Quentin
  • 62,093
  • 7
  • 131
  • 191
xDGameStudios
  • 321
  • 1
  • 13
  • If you want to designate a method in your case, you'll have to speak about the method of an instantiated template: `TExample::my_method`, for instance. – Laurent LA RIZZA Apr 04 '19 at 14:20
  • What's the error? C++ doesn't care that a class is an instance of a template, so there's no reason why this shouldn't work on its side. – Quentin Apr 04 '19 at 15:03
  • @Quentin yes there is.. is related to the framework I'm using... is from Godot... – xDGameStudios Apr 04 '19 at 15:14
  • Yes, I understood that much. But please copy-paste the complete and exact error you're getting, since as @rustyx demonstrated the issue is not on the calling side. – Quentin Apr 04 '19 at 15:28
  • Sorry about the confusion, I don't want to know why it fails.. it fails because the framework doesn't support it and the engine doesn't support it either :( I wanted to know if there is a short cut for the code at the bottom of OP – xDGameStudios Apr 05 '19 at 11:22

3 Answers3

2

Not sure what you mean by "this fails".

It just works, look (live demo):

template<class T>
class TExample {
public:
    void my_method() {}
};

class Example : TExample<int> {
    template<class U>
    static void register_method(U u) {
    }
public:
    static void register_methods() {
        register_method(&Example::my_method); // it works
        register_method(&TExample::my_method); // this also works
    }
};

int main()
{
    Example ex;
    ex.register_methods();
}

Now if you want to access my_method() from outside the class, then you should inherit publicly:

class Example : public TExample<...>
{

Then Example::my_method() will also work outside.


Note: TExample is not a template class, but a class template. However, in the context of a template instantiation (inside the definition of Example) the template arguments are substituted automatically.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • By fails I mean fails to even compile... As I said the method "register_methods" is a complicated beast under the hood and doesn't like to receive pointer to classes that are templates... better saying for it to work I need to register the class template and the framework doesn't allow that... – xDGameStudios Apr 04 '19 at 14:59
  • 2
    In that case can you come up with a minimal example that actually reproduces your issue. Otherwise I don't know what the issue is. – rustyx Apr 04 '19 at 15:22
  • Sorry about the confusion, I don't want to know why it fails.. it fails because the framework doesn't support it and the engine doesn't support it either :( I wanted to know if there is a short cut for the code at the bottom of OP – xDGameStudios Apr 05 '19 at 11:23
0

Since template classes will be created for the types you are using you should mention the type also.

register_method("my_method", &TMyClass<Type>::my_method);
Petar Velev
  • 2,305
  • 12
  • 24
  • Followed your suggestion the problem persists. Like I said the framework doesn't support pointer to methods in template classes. – xDGameStudios Apr 04 '19 at 14:46
0

What about using a lambda ?

Does:

register_method("my_method", [&obj](*whateverParam*) { obj.templateMethod(*whateverParam*); } );

work?

(assuming obj contains the actual method, but that could be replaced by any instance containing the method).

Naliwe
  • 322
  • 1
  • 8