【发布时间】:2018-07-09 23:34:20
【问题描述】:
我在玩std::for_each,我可以理解以下内容:
std::vector<int> nums{1,2,3,4,5,6};
std::for_each(nums.begin(), nums.end(), [](int& n) {
n++;
});
根据定义,UnaryFunction f 不难扣除。
template<class InputIt, class UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f);
但是,当我有一个线程向量时
std::vector<std::thread> threads;
我对这行阻止并等待执行完成的特定代码行着迷:
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join))
它是如何工作的?在这种情况下,UnaryFunction 是什么? std::thread::join 不带参数,因此它不是一元函数。 std::mem_fn 有什么魔力来创建一元函数?
【问题讨论】: