C++ 构造函数中的Lambda
我有一个在其构造函数中使用lambda的类,我很难理解它是如何执行的C++ 构造函数中的Lambda,c++,C++,我有一个在其构造函数中使用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
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 s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};
如果我创建一个类型为check的对象,请使用以下命令
check db_intl{"Test", db_dur};
调用以下构造函数
check::check(std::string s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};
如果lambda没有被使用,这个构造函数如何调用另一个具有lambda的构造函数?由于
pred
被类型化为std::function
,lambda将被转换为pred
,然后构造被委托给第三个构造函数(检查(std::string,pred,msec duration=msec{0}
。然后,lambda将存储在\u谓词
容器中,并(可能)在稍后调用谓词时调用。\u谓词被定义为pred\u list const\u谓词;
如果我创建一个类型为check的对象,如check db\u intl{“Test”,db\u dur};
它只传递2个参数,在第三个构造函数中没有可用于lambda@zer0c00l双参数构造函数将委托给三参数构造函数;请参阅上的“委托构造函数”部分。lambda未使用任何局部变量。
check::check(std::string s, msec dur)
: check(s, [](int i) { return i > 0; }, dur)
{};