如何使用成员函数指针创建模板类?

时间:2016-05-29 16:50:31

标签: c++ templates member-function-pointers

有谁知道如何为下一个模板专业化声明通用模板表单:

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

};

Apply必须是指向成员函数的指针,在模板声明中,签名是未知的。

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

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;
}

举个例子:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

    Log.d("FROM", "From: " + remoteMessage.getFrom() );
    sendNotification(remoteMessage.getNotification().getBody());
}

确实非常难看,但这就是OP(也许)所要求的。

相关问题