-2

Is anyone knows how to declare generalized template form for the next template specialization:

template <template <class> class Container,
          class Value,
          class Return,
          Return (Container<Value>::*Apply)(const Value &)>
class Example<Container<Value>, Apply>
{

};

Apply must be a pointer to a member function whoes signature is unknown in template declaration.

Razial
  • 109
  • 2
  • 7

1 Answers1

0

Do you mean something like this?

template<typename T, typename F>
struct S;

template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
    using Apply = R(C<A...>::*)(A...);
     // ...
};

As an example:

template<typename... U>
struct T {
    int f(int, char) { return 42; }
};

template<typename T, typename F>
struct S;

template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
    using Apply = R(C<A...>::*)(A...);
     // ...
};

int main() {
    S<T<int, char>, int(int, char)>::Apply apply = &T<int, char>::f;
}

Pretty ugly, indeed, but that's what the OP (maybe) asked.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • It's difficult to say whether it helps me or not because i'm never worked with C++11 aliasing. The idea is to get member function address at compile time rather than store address in variable. – Razial May 29 '16 at 17:58
  • To get a member function address for a given object you need an instance of a class, but you can still get the member function pointer type at compile time. This snippet does that and gives it the name `Apply`. – skypjack May 29 '16 at 21:18
  • Really not. There is no need in instance of a class to get member function pointer. Try this code: `template class T { public: void func(AnyExistingClass *obj) { obj->*f(); } };`. It's work. – Razial May 30 '16 at 04:05
  • I agree. It works and `f` is the name you gave to the member function pointer. If you find that `Apply` doesn't fit well, feel free to use `f`. – skypjack May 30 '16 at 05:27