Q: Dynamically create a function and get a pointer I am using Arduino and motor encoders to track the rotations of a motor. To do this, I am using interrupts on the Arduino. I can create a function, an ISR, that will be executed by the processor whenever the signal changes on a pin. That Interrupt/ISR combinations works like this:
void setup() {
    attachInterrupt(1,ISR_function,FALLING);
}

void ISR_function() {
    // do something
}

Seeing as I have multiple motors with encoders, I decided I would make a class to handle this. However, the attachInterrupt method requires a function pointer, and I am aware that in C++ you cannot have a pointer to a method function of an instance of an object. So something like this will not work:
class Encoder {
    public:
        Encoder(void);
        void ISR_function(void);
    private:
        // Various private members
}

Encoder::Encoder() {
    attachInterrupt(1,ISR_function,FALLING);
}

Encoder::ISR_function() {
    // Do some interrupt things with private members
}

Because ISR_function is not static. The ISR_function however executes code that is dependent on the the private data members of each specific instance.
Is it possible to create a function dynamically? And then retrieve a pointer to that function? Almost like in javascript:
class Encoder {
    public:
        Encoder(void);
        void* ISR_function(void);
    private:
        // Various private members
}

Encoder::Encoder() {
    attachInterrupt(1,ISR_function(),FALLING);
}

Encoder::ISR_function() {
    return dynamicFunctionPointer;
}

Is this possible? If not, how can accomplish what I am trying to do without manually creating separate static ISR_functions.

A: // type of an interrupt service routine pointer
using ISR = void(*)();

// a fake version of the environment we are working with
// for testing purposes
namespace fake_environment {
    enum bob{FALLING};

    ISR isrs[100] = {0};

    void attachInterrupt(int i, void(*f)(), bob) {
        isrs[i] = f;
    }

    void runInterrupt(int i) {
        isrs[i]();
    }
}

// type storing a pointer to member function
// as a compile-time constant
template<class T, void(T::*m)()>
struct pmf {};

// stores a pointer to a class instance
// and a member function.  Invokes it
// when called with operator().  Type erases
// stuff down to void pointers.
struct funcoid {
  using pfunc = void(*)(void*);
  pfunc pf = 0;
  void* pv = 0;
  void operator()()const { pf(pv); }
  template<class T, void(T::*m)()>
  funcoid(T* t, pmf<T,m>):
    pv(t)
  {
    // create a lambda, then decay it into a function pointer
    // this stateless lambda takes a void* which it casts to a T*
    // then invokes the member function m on it.
    pf = +[](void* pt) {
      (static_cast<T*>(pt)->*m)();
    };
  }
  funcoid()=default;
};

// a global array of interrupts, which have a this pointer
// and a member function pointer type erased:
namespace client {
  enum {interrupt_count = 20};
  std::array<funcoid, interrupt_count> interrupt_table = {{}};
  // with a bit of work, could replace this with a std::vector        
}

// some metaprogramming utility code
// this lets me iterate over a set of size_t at compile time
// without writing extra helper functions at point of use.
namespace utility {
  template<std::size_t...Is>
  auto index_over( std::index_sequence<Is...> ) {
    return [](auto&& f)->decltype(auto) {
      return f(std::integral_constant<std::size_t, Is>{}...);
    };
  }
  template<std::size_t N>
  auto index_upto( std::integral_constant<std::size_t, N> ={} ) {
    return index_over( std::make_index_sequence<N>{} );
  }
}

// builds an array of interrupt service routines
// that invoke the same-index interrupt_table above.
namespace client {
  // in g++, you'd write a helper function taking an `index_sequence`
  // and take the code out of that lambda and build the array there:
  std::array<ISR, interrupt_count> make_isrs() {
    // creates an array of ISRs that invoke the corresponding element in interrupt_table.
    // have to do it at compile time, because we are generating 20 different functions
    // each one "knows" its index, then storing pointers to them.
    // Could be done with a lot of copy-pasta or a macro
    return ::utility::index_upto< interrupt_count >()(
      [](auto...Is)->std::array<ISR, interrupt_count>{
        return {{ []{ interrupt_table[decltype(Is)::value](); }... }};
      }
    );
  }
  // isr is a table of `void(*)()`, suitable for use
  // by your interrupt API.  Each function pointer "knows" its
  // index, which it uses to invoke the appropraite `interrupt_table`
  // above.
  auto isr = make_isrs();
  // with a bit of work, could replace this with a std::vector        
}

// interrupt is the interrupt number
// index is the index in our private table (0 to 19 inclusive)
// t is the object we want to use
// mf is the member function we call
// kind is FALLING or RISING or the like
// index must be unique, that is your job.
template<class T, void(T::*m)()>
void add_interrupt( int interrupt, int index, T* t, pmf<T, m> mf, fake_environment::bob kind ) {
  client::interrupt_table[index] = {t, mf};
  fake_environment::attachInterrupt(interrupt,client::isr[index],kind);
}


class Encoder {
  public:
    Encoder():Encoder(1, 7) {};
    Encoder(int interrupt, int index);
    void ISR_function(void);
    // my choice for some state:
    std::string my_name;
};

Encoder::Encoder(int interrupt, int index) {
  add_interrupt( interrupt, index, this, pmf<Encoder, &Encoder::ISR_function>{}, fake_environment::FALLING );
}

void Encoder::ISR_function() {
  // display state:
  std::cout << my_name << "\n";
}

int main() {
  Encoder e0;
  e0.my_name = "Hello World";
  fake_environment::runInterrupt(1);
  Encoder e1(0, 10);
  e1.my_name = "Goodbye World";
  fake_environment::runInterrupt(0);
}

Does not compile in g++ and uses C++14.
Does solve your problem.  g++ problem is in make_isrs, which can be replaced by verbose copy-paste initialization.  C++14 is from index_upto and index_over, which can similarly be reworked for C++11.
Live example.
However, ISRs are supposed to be minmal; I suspect you should just record the message and handle it elsewhere instead of interacting with object state.

A: To call a member function you need an instance to invoke it on, so it doesn't seem like a good choice to use for interrupts. 
From pointers-to-members:
A member function is meaningless without an object to invoke it on.
Non-static member functions have a hidden parameter that corresponds to the this pointer. The this pointer points to the instance data for the object. The interrupt hardware/firmware in the system is not capable of providing the this pointer argument. You must use "normal" functions (non class members) or static member functions as interrupt service routines.
One possible solution is to use a static member as the interrupt service routine and have that function look somewhere to find the instance/member pair that should be called on interrupt. Thus the effect is that a member function is invoked on an interrupt, but for technical reasons you need to call an intermediate function first.

A: First of all, you can extract pointer to a class method and call it:
auto my_method_ptr = &MyClass::my_method;
....
(myClassInstance->*my_method_ptr)(); // calling via class ptr
(myclassInstance.*my_method_ptr)(); // calling via class ref

This basically passes myClassInstance pointer to MyClass::my_method as an implicit argument, accessible via this.
Unfortunately, AVR interrupt controller can't call class method, as the hardware operate on simple pointers only and can't call that method with implicit argument. You'll need a wrapper function for this.
MotorEncoderClass g_motor; // g_ for global

void my_isr() {
    g_motor.do_something();
}

int main() {
    // init g_motor with relevant data
    // install my_isr handler
    // enable interrupts
    // ... do rest of stuff
    return 0;
}



*

*Create your class instance as a global variable.

*Create ordinary function that calls that method

*Initialize your motor class with relevant data

*Install my_isr as IRQ handler.

*Press start to begin :)

