C++ VisualStudio2005中的Lambda函数替换——Boost库
下面最后一行将不会在Visual Studio 2005中编译:C++ VisualStudio2005中的Lambda函数替换——Boost库,c++,multithreading,boost,lambda,C++,Multithreading,Boost,Lambda,下面最后一行将不会在Visual Studio 2005中编译: std::deque<int> q; boost::condition_variable cond; boost::mutex mu; boost::unique_lock<boost::mutex> locker(mu); cond.wait(locker, [](){ return !q.empty();} ); // Unlock mu and wait to be notified 顺便说一句,
std::deque<int> q;
boost::condition_variable cond;
boost::mutex mu;
boost::unique_lock<boost::mutex> locker(mu);
cond.wait(locker, [](){ return !q.empty();} ); // Unlock mu and wait to be notified
顺便说一句,UntilEmpty通常被称为函子。Boost的Lambda带有Boost.Lambda和Boost.Phoenix。@chris:但是编译器不需要支持语法吗?如果没有,我应该包含哪些Boost头来编译上述代码?不,没有。这就是它的美妙之处。Boost只用C++03就可以完成一些非常棒的事情。无论如何,去看看文档(和教程)。这包括标题。很抱歉,我不小心遗漏了队列的声明。我编辑了我的原始问题来添加它。你的解决方案上面的工作。仍然有兴趣通过上面Chris的建议添加lambda支持,但这比简单地添加boost标题更复杂:#包括“boost\lambda\lambda.hpp”和#包括“boost\phoenix\phoenix.hpp”。我将按照他的建议查看lambda教程。
class condition_variable:
private detail::basic_condition_variable
{
public:
BOOST_THREAD_NO_COPYABLE(condition_variable)
condition_variable()
{}
using detail::basic_condition_variable::notify_one;
using detail::basic_condition_variable::notify_all;
void wait(unique_lock<mutex>& m)
{
do_wait(m,detail::timeout::sentinel());
}
template<typename predicate_type>
void wait(unique_lock<mutex>& m,predicate_type pred)
{
while(!pred()) wait(m);
}
error C2059: syntax error : '['
error C2143: syntax error : missing ')' before '{'
error C2143: syntax error : missing ';' before '{'
class UntilEmpty
{
public:
UntilEmpty(std::deque<int>& t) : q(t) {}
bool operator() () { return !q.empty(); }
private:
std::deque<int>& q;
};
UntilEmpty until_empty(q);
cond.wait(locker, until_empty);