C ++ lambda转换-为什么候选构造函数不可行:没有已知的从lambda到std :: function的转换 [英] C++ lambda conversion -- why is candidate constructor not viable: no known conversion from lambda to std::function
本文介绍了C ++ lambda转换-为什么候选构造函数不可行:没有已知的从lambda到std :: function的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关隐式lambda转换的问题.我有这种类型:
Question about implicit lambda conversions. I have this type:
class A {
public:
A(std::function<void(std::string)> func) {
....
}
};
我相信其中有一个有效的副本构造函数.
Which I believe has a valid copy constructor.
我想执行以下操作
A a = [](std::string param) { ... };
或
void MyFunc(A a) { ... } // definition
MyFunc([](std::string param) { ... }); // call
但是这两个都会产生编译错误:
Yet both these yield compile error:
注意:候选构造函数不可行:第一个参数没有从'(...处的λ)到'std :: function'的已知转换
这是为什么?还是应该这样?
Why is this? Or should this be possible?
推荐答案
您的问题是只允许一个用户转换,而您需要两个用户转换:
Your problem is that only one user conversion is allowed and you need two:
- lamba->
std::function
->A
.
- lamba ->
std::function
->A
.
两者
A a{[](std::string) {}};
MyFunc({[](std::string) {}});
工作.
这篇关于C ++ lambda转换-为什么候选构造函数不可行:没有已知的从lambda到std :: function的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文