【发布时间】:2021-02-13 21:28:16
【问题描述】:
假设您有一些使用 std::bind 包装的函数和一些有界参数:
void func1(std::string& arg1, std::unique_ptr<int>& arg2)
{
...
}
...
auto f1 = std::bind(func1, std::string{"message"}, std::make_unique<int>(123));
现在,想象一下,无论出于何种原因,您想将此对象重新绑定到另一个具有一些附加参数的函数,例如:
void func2(std::exception& ex, std::string& arg1, std::unique_ptr<int>& arg2)
{
...
}
...
try {
f1();
} catch (std::exception& ex) {
auto f2 = some_magical_rebind(func2, std::move(ex), args_of(f1));
}
有可能这样做吗?正如std::bind 在内部使用std::tuple 来打包它认为应该可行的参数。
【问题讨论】:
-
不符合标准,未指定
std::bind的结果类型 -
"由于 std::bind 在内部使用 std::tuple 来打包参数" 这是标准绝不要求的实现细节。
-
您有什么问题 - 将另一个函数重新绑定到
f1或提取func1()的参数? -
当且仅当您设计自己的函数和参数持有者对象时才有可能,例如使用 std::tuple。
-
我对这个问题的前提感到困惑。 try-catch 块的上下文是否知道
f1的签名(f2很明显)?那为什么要绑定参数呢?