A-n*_*n-y 7 c++ lambda function-pointers default-parameters std-function
有没有办法从lambda恢复类型信息,默认参数存储在std :: function中,该类型的参数中没有这些参数?
std::function<void()> f1 = [](int i = 0){};
std::function<void(int)> f2 = [](int i = 0){};
std::function<void(int)> f3 = f1; // error
std::function<void()> f4 = f2; // error
Run Code Online (Sandbox Code Playgroud)
查看std :: function的复制构造函数,其他函数类型没有部分模板特化,所以我想这个信息会丢失,只是你不能将一种类型的函数赋值给一个函数另一种类型,即使在内部它们都可以调用该函数.它是否正确?是否有任何解决方法可以实现这一目标?我正在看std :: function :: target,但没有运气,我不是函数类型和指针的专家.
另外,f1(或lambda)如何绑定默认参数?
不,这是不可能的,因为默认参数是函数声明集的属性,而不是函数本身的属性。换句话说,这是完全合法的C ++:
丙型肝炎
int f(int i = 42);
const int j = f(); // will call f(42)
Run Code Online (Sandbox Code Playgroud)
丙型肝炎
int f(int i = 314);
const int k = f(); // will call f(314)
Run Code Online (Sandbox Code Playgroud)
丙型肝炎
int f(int i = 0)
{
return i;
}
const int x = f(); // will call f(0)
Run Code Online (Sandbox Code Playgroud)
这些都可以链接在一起就好了。
这意味着不可能以某种方式从函数中“检索”默认参数。
您可以等效于f4 = f2使用std::bind和提供自己的默认参数,如下所示:
std::function<void()> f4 = std::bind(f2, 42);
Run Code Online (Sandbox Code Playgroud)
但是,无法获得与相等的东西f3 = f1。
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |