Search code examples
c++c++11static-methods

Automated call to class static functions C++11


I'm working on a project executed on an ESP32 micro controller (c++11) and I would like to implement a class method that could take Class References (not instances of those classes) as arguments in order to execute static methods of those classes.

I manage to implement the following working solution :

#include <iostream>

    class Model
{
public:
    static void foo()
    {
        std::cout << "Model.foo invoked" << std::endl;
    };
};

class ModelA: public Model
{
public:
    static void foo()
    {
        std::cout << "ModelA.foo invoked" << std::endl;
    };
};

class ModelB: public Model
{
public:
    static void foo()
    {
        std::cout << "ModelB.foo invoked" << std::endl;
    };
};


class Manager
{
public:

    template <class T = Model>
    void callFoo()
    {
        T::foo();
    }

    template<class T = Model, class ... args>
    void setup()
    {
       callFoo<T>();
       callFoo<args...>();
    }

};


int main()
{

    Manager manager;
    manager.setup<ModelA, ModelB>();

    return 0;
}

The output meets the expected result :

ModelA.foo invoked

ModelB.foo invoked

But I have the feeling that this approach is at best a poor hack...

Does someone has a better way to implement this (ideally without using a template) ?

Thank you in advance.

Vincent.


Solution

  • If you want to dispatch static member functions of any type based on references of that type, you can easily do this by (slightly) abusing function template parameter inference:

    template <typename T>
    void call(T const& = *static_cast<T const*>(nullptr)) {
      T::foo();
    }
    
    struct Magic {
      static void foo() {}
    };
    struct MoreMagic {
      static void foo() {}
    };
    
    int main() {
      call<Magic>();
      call(MoreMagic());
      MoreMagic m;
      call(m);
    }