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

How to create template class with member function pointer?

本文关键字:建模 创建 指针 何使用 成员 函数      更新时间:2023-10-16

>有没有人知道如何为下一个模板专业化声明通用模板形式:

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

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

你的意思是这样吗?

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...);
     // ...
};

举个例子:

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

确实很丑,但这就是OP(也许)提出的要求。