构造函数中的 Lambda
Lambda in constructor
>我有一个在其构造函数中使用lambda的类,我很难理解它是如何执行的
using pred = std::function<bool(int)>;
using pred_list = std::vector<pred>;
class check
{
private:
std::string const _description;
public:
check(std::string, msec duration = msec{0});
check(std::string, pred_list, msec duration = msec{0});
check(std::string, pred, msec duration = msec{0});
};
构造函数
check::check(std::string d, pred_list p, msec dur)
: _description{d}, _duration{dur}, _predicates{p}, _pred_pass{false}
, _deadline{msec::max()}
{};
check::check(std::string d, pred p, msec dur)
: _description{d}, _duration{dur}, _predicates{1,p}, _pred_pass(false)
{};
check::check(std::string s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};
有问题的构造函数
check::check(std::string s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};
如果我使用以下方法创建类型检查的对象
check db_intl{"Test", db_dur};
调用以下构造函数
check::check(std::string s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};
如果未使用另一个构造函数,此构造函数如何能够使用 lambda 调用该构造函数?
由于 pred
的类型定义为 std::function<bool(int)>
,lambda 将被转换为 pred
,然后构造委托给第 3 个构造函数 (check(std::string, pred, msec duration = msec{0})
。 然后,lambda 将存储在 _predicates
容器中,并在稍后调用谓词时(大概(调用。
相关文章:
- 在构造函数中使用 lambda 的 C++ 类
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 使用 lambda 作为构造函数参数是否需要C++ 17?
- 构造函数中的 Lambda
- 当命名的 lambda 用作模板类参数或构造函数参数时,类模板无法编译
- Lambda 闭包类型构造函数
- 如何从传递给友好类构造函数的 lambda 访问私有成员
- 如何将lambda函数传递到具有未知类型和未知参数的构造函数
- 将lambda传递到成员变量的构造函数
- C 使用lambda进行隐式构造函数呼叫期望函数指针
- Lambda构造函数中的实例化
- 为什么在Lambda移动时未调用移动构造函数
- C++将Lambda函数作为类构造函数参数传递
- 在可移动类型的构造函数 lambda 中捕获此内容的安全使用
- VC 15调用错误的复制构造函数以捕获lambda
- C++ lambda 构造函数参数是否可以捕获构造的变量
- 构造函数初始化列表中可变参数的Lambda捕获
- 构造函数初始值设定项列表中的 Lambda 可以使用哪些标识符
- 如何将函数和参数包转发到 lambda 中,转发到 std::thread 的构造函数中