从绑定与 lambda 函数中获取 operator() 的类型
getting the type of operator() from a bind versus a lambda function
请看下面的代码。在捕获 lambda 表达式的情况下,它将工作并编译得很好。虽然在使用绑定可调用表达式时我会得到一个重载表达式表达式错误
main.cpp:13:60: error: decltype cannot resolve address of overloaded function
using FuncType = decltype(&std::decay<T>::type::operator());
我如何编写代码才能获得绑定函数的 operator() 方法的类型?绑定类型是 std::_Bind<void (*(std::_Placeholder<2>, std::_Placeholder<1>))(int, int)>
,我也不完全理解;我知道 void (*) (int, int) 是一种类型,它是指向接受 int、int 并返回 void 的函数的指针。我想我不明白语法 void *(x,y) (int,int);基本上是 x,y 部分。我假设在所有模板内容之后有一个方法 void operator()(int, int) 将得到解决 bind_f(x,y) 将调用并且我试图捕获该类型。
#include <iostream>
#include <functional>
#include <type_traits>
void tester(int x, int y) {
std::cout << " x = " << x << " y = " << y << std::endl;
}
template <typename T>
class TypeChecker;
template <typename T>
using FuncType = decltype(&std::decay<T>::type::operator());
int main() {
using namespace std::placeholders;
auto bind_f = std::bind(tester, _2, _1);
bind_f(1,2);
int y = 5;
auto lambda = [y]() {
std::cout << " y = " << y << std::endl;
};
typedef FuncType<decltype(lambda)> x1;
typedef FuncType<decltype(bind_f)> x2;
//TypeChecker<decltype(bind_f)> t2;
}
正如评论者所说,bind
对象上的 operator()
是一个模板化(即重载)函数,您不能简单地 bind
而不选择您想要的版本。
我们可以通过将另一个模板参数添加到 FuncType
、static_cast
到 select 适当的函数来解决这个问题,然后在我们制作 typedef 时明确说明:
template <typename T, typename... U>
using FuncType = decltype(static_cast<void(T::*)(U...) const>(&std::decay<T>::type::operator()));
然后
auto bind_f = std::bind(tester, _2, _1);
int y = 5;
auto lambda = [y]() {
std::cout << " y = " << y << std::endl;
};
using x1 = FuncType<decltype(lambda)>;
using x2 = FuncType<decltype(bind_f), int&&, int&&>;
x1 p = &decltype(lambda)::operator();
(lambda.*p)();
x2 q = &decltype(bind_f)::operator();
(bind_f.*q)(1,2);
输出:
y = 5
x = 2 y = 1
我同意 完全避免 bind
; lambda 让事情变得更简单。
请看下面的代码。在捕获 lambda 表达式的情况下,它将工作并编译得很好。虽然在使用绑定可调用表达式时我会得到一个重载表达式表达式错误
main.cpp:13:60: error: decltype cannot resolve address of overloaded function
using FuncType = decltype(&std::decay<T>::type::operator());
我如何编写代码才能获得绑定函数的 operator() 方法的类型?绑定类型是 std::_Bind<void (*(std::_Placeholder<2>, std::_Placeholder<1>))(int, int)>
,我也不完全理解;我知道 void (*) (int, int) 是一种类型,它是指向接受 int、int 并返回 void 的函数的指针。我想我不明白语法 void *(x,y) (int,int);基本上是 x,y 部分。我假设在所有模板内容之后有一个方法 void operator()(int, int) 将得到解决 bind_f(x,y) 将调用并且我试图捕获该类型。
#include <iostream>
#include <functional>
#include <type_traits>
void tester(int x, int y) {
std::cout << " x = " << x << " y = " << y << std::endl;
}
template <typename T>
class TypeChecker;
template <typename T>
using FuncType = decltype(&std::decay<T>::type::operator());
int main() {
using namespace std::placeholders;
auto bind_f = std::bind(tester, _2, _1);
bind_f(1,2);
int y = 5;
auto lambda = [y]() {
std::cout << " y = " << y << std::endl;
};
typedef FuncType<decltype(lambda)> x1;
typedef FuncType<decltype(bind_f)> x2;
//TypeChecker<decltype(bind_f)> t2;
}
正如评论者所说,bind
对象上的 operator()
是一个模板化(即重载)函数,您不能简单地 bind
而不选择您想要的版本。
我们可以通过将另一个模板参数添加到 FuncType
、static_cast
到 select 适当的函数来解决这个问题,然后在我们制作 typedef 时明确说明:
template <typename T, typename... U>
using FuncType = decltype(static_cast<void(T::*)(U...) const>(&std::decay<T>::type::operator()));
然后
auto bind_f = std::bind(tester, _2, _1);
int y = 5;
auto lambda = [y]() {
std::cout << " y = " << y << std::endl;
};
using x1 = FuncType<decltype(lambda)>;
using x2 = FuncType<decltype(bind_f), int&&, int&&>;
x1 p = &decltype(lambda)::operator();
(lambda.*p)();
x2 q = &decltype(bind_f)::operator();
(bind_f.*q)(1,2);
输出:
y = 5
x = 2 y = 1
我同意 bind
; lambda 让事情变得更简单。