gpt4 book ai didi

c++ - 从绑定(bind)与 lambda 函数中获取 operator() 的类型

转载 作者:太空狗 更新时间:2023-10-29 23:14:16 28 4
gpt4 key购买 nike

请看下面的代码。在捕获 lambda 表达式的情况下,它将工作并编译得很好。虽然在使用绑定(bind)可调用表达式时我会得到一个重载表达式表达式错误

main.cpp:13:60: error: decltype cannot resolve address of overloaded function 
using FuncType = decltype(&std::decay<T>::type::operator());

我如何编写代码才能获得绑定(bind)函数的 operator() 方法的类型?绑定(bind)类型是 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'ing 以选择适当的函数,然后在我们创建 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);

Live Demo

输出:

y = 5

x = 2 y = 1

我同意 Nir Friedman when he said完全避免 bind; lambda 让事情变得更简单。

关于c++ - 从绑定(bind)与 lambda 函数中获取 operator() 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com