仅替换成员函数的模板语法
Template syntax to substitute only member functions
我试图创建一个函数重载,以便它只绑定(适用于)成员函数。我看了一下std::mem_fn
的功能签名 http://en.cppreference.com/w/cpp/utility/functional/mem_fn
template <class Ret, class T>
/* unspecified */ mem_fn (Ret T::* pm);
所以我这样构建我的参数
template <typename R, typename F>
auto call_me(R C::* func) {
return (mContainer.*func);
}
但是,我随后收到此错误
.template.cpp: In function 'int main()':
.template.cpp:29:49: error: no matching function for call to 'MyClass<int, std::vector<int> >::call_me(std::vector<int>::size_type (std::vector<int>::*)() const noexcept)'
cout << test.call_me(&std::vector<int>::size) << endl;
^
.template.cpp:16:10: note: candidate: template<class R, class F> auto MyClass<T, C>::call_me(R C::*) [with R = R; F = F; T = int; C = std::vector<int>]
auto call_me(R C::* func) {
^~~~~~~
.template.cpp:16:10: note: template argument deduction/substitution failed:
.template.cpp:29:49: note: couldn't deduce template parameter 'F'
cout << test.call_me(&std::vector<int>::size) << endl;
我尝试这样做的原因是,我可以有一个适用于一般 lambda 和函数对象的重载,以及另一个适用于成员函数指针的重载。这是我试图实现的目标的最小示例。我知道这个问题有点令人困惑,所以如果需要,请随时要求澄清。
#include <vector>
#include <iostream>
using namespace std;
template <typename T, typename C>
struct MyClass {
// This doesnt work because of SFINAE
template <typename F, typename... A>
auto call_me(F func, A... args) { // lambda version
return func(args...);
}
template <typename R, typename F>
auto call_me(R C::* func) { // member function version
return (mContainer.*func);
}
C mContainer; // this is private in my actual code
};
int main() {
MyClass<int, std::vector<int> > test;;
// these two calls will call the member function version of the overload
cout << test.call_me(&std::vector<int>::size) << endl;
using insert_func_t = std::vector<int>::iterator(std::vector<int>::*)(std::vector<int>::const_iterator, const int&);
test.call_me(static_cast<insert_func_t>(&std::vector<int>::insert), test.mContainer.begin(), 4);
// this call will call the lambda version of the overload
cout << test.call_me([](std::vector<int>& in){ in.push_back(5); });
return 0;
}
你可以用std::invoke
来涵盖这两种情况:
template <typename F, typename... A>
auto call_me(F func, A... args) { // lambda version
return std::invoke(func, mContainer, args...);
}
对于函数对象,例如闭包,这将调用operator()
。对于成员函数,它会在参数前面加上要使用的对象,然后使用适当的语法调用它。换句话说,你的工作已经完成了。
您还可以考虑完美的转发:
template <typename F, typename... A>
auto call_me(F&& func, A&&... args) { // lambda version
return std::invoke(std::forward<F>(func), mContainer, std::forward<A>(args)...);
}
函数
(或类方法)比返回类型更多。
函数有参数。每个参数都有一个类型。函数参数的类型是函数签名的一部分。这包括具有零参数的函数。
如果希望模板函数绑定到任意类的成员函数,则模板函数还必须显式绑定到类成员函数的参数类型。这是C++类型安全的一个基本方面:
template<class Ret,
class T,
class ...Args>
void mem_fn(Ret (T::*pm)(Args...))
{
}
class foo {
public:
void bar(int);
};
void foo()
{
mem_fn(&foo::bar);
}
如果您有兴趣仅绑定到不带参数的成员函数,则可以放弃可变参数模板参数,并且必须相应地显式绑定模板:
template<class Ret,
class T>
void mem_fn(Ret (T::*pm)())
{
}
class foo {
public:
void bar();
};
void foo()
{
mem_fn(&foo::bar);
}
请注意,此模板将仅绑定到不带参数的类成员函数。例如,它不会绑定到 void bar(int)
成员函数。
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- 使用基类指针调用基类的值构造函数的语法是什么?
- C++使用 rand 定义函数语法
- C++中未命名函数指针的语法
- 将值传递给构造函数 c++ 的差异语法
- C++语法中的函数指针
- 存储函数指针的正确语法
- QObject::连接不起作用 - 使用函数语法找不到信号
- 对函数库中的语法感到困惑 std::bind
- 解释通过从函数引用返回数组的语法
- 函数错误 C2059:语法错误:'>'不起作用
- 当C++类函数参数之一是结构时,它们的语法有什么不同
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- Qt的新信号/时隙语法问题 - 连接到一个简单的函数
- 需要"模板<>"语法 --> 通过函数调用类模板
- C++ std::函数语法问题
- 备用函数语法/函数原型
- 奇怪的c++语法:函数调用之前的类型定义
- c++数组语法(函数返回数组)
- 实现[B,C]=f(A)语法(函数f作用于具有两个或多个输出数组的数组)