【问题标题】:Compilation error on using boost::future .then()使用 boost::future .then() 时的编译错误
【发布时间】:2013-10-19 10:00:29
【问题描述】:

我正在尝试使用 boost::future .then() 功能。 sn-p 取自Boost 1.54.0 thread synchronisation documentation

#include <string>  
#include <boost/thread/future.hpp>
int main() {
  boost::future<int> f1 = boost::async([]() { return 123; });
  boost::future<std::string> f2 = f1.then([](boost::future<int> f)->std::string {
                                            int x = f.get();
                                            return ("Done" + std::to_string(x));
                                            });
}

设置:
Ubuntu 13.04
g++ 版本 g++ (Ubuntu 4.8.1-2ubuntu1~13.04) 4.8.1
提升版本 1.54.0

命令行:

g++  then_test.cc -std=c++0x -DBOOST_THREAD_VERSION=4 -I        /home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost -L /home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/stage/lib -static -lboost_thread-mt -lboost_date_time-mt  -lboost_system-mt -lpthread

错误:

g++  then_test.cc -std=c++0x -DBOOST_THREAD_VERSION=4 -I /home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost -L /home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/stage/lib -static -lboost_thread-mt -lboost_date_time-mt  -lboost_system-mt -lpthread
then_test.cc: In function ‘int main()’:
then_test.cc:10:44: error: no matching function for call to ‘boost::future<int>::then(main()::__lambda1)’
                                           });
                                            ^
then_test.cc:10:44: note: candidates are:
In file included from then_test.cc:2:0:
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:1598:9: note: template<class F> boost::future<typename boost::result_of<F(boost::future<R>&)>::type> boost::future<R>::then(F&&) [with F = F; R = int]
         then(BOOST_THREAD_FWD_REF(F) func);
         ^
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:1598:9: note:   template argument deduction/substitution failed:
In file included from then_test.cc:2:0:
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp: In substitution of ‘template<class F> boost::future<typename boost::result_of<F(boost::future<R>&)>::type> boost::future<R>::then(F&&) [with F = F; R = int] [with F = main()::__lambda1]’:
then_test.cc:10:44:   required from here
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:62:29: error: no type named ‘type’ in ‘struct boost::result_of<main()::__lambda1(boost::future<int>&)>’
 #define BOOST_THREAD_FUTURE future
                             ^
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:3840:3: note: in expansion of macro ‘BOOST_THREAD_FUTURE’
   BOOST_THREAD_FUTURE<R>::then(BOOST_THREAD_FWD_REF(F) func)
   ^
In file included from then_test.cc:2:0:
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:1601:9: note: template<class F> boost::future<typename boost::result_of<F(boost::future<R>&)>::type> boost::future<R>::then(boost::launch, F&&) [with F = F; R = int]
         then(launch policy, BOOST_THREAD_FWD_REF(F) func);
         ^
/home/prakash/maidsafe/MaidSafe/build/boost_1_54_0/src/boost/boost/thread/future.hpp:1601:9: note:   template argument deduction/substitution failed:
then_test.cc:10:44: note:   cannot convert ‘<lambda closure object>main()::__lambda1{}’ (type ‘main()::__lambda1’) to type ‘boost::launch’
                                           });

如果我在这里遗漏了什么,请告诉我。

【问题讨论】:

  • Boost.ResultOf 默认不使用decltype,因此不能与 lambda 一起使用。 #define BOOST_RESULT_OF_USE_DECLTYPE 在包含 Boost 标头之前,最好是项目范围内的。

标签: c++ boost c++11 boost-thread continuations


【解决方案1】:

通过引用 .then() 传递 future 修复了 gcc 4.8 和 clang 上的编译问题。

对于 windows 和 gcc 4.7,我们还需要定义 BOOST_RESULT_OF_USE_DECLTYPE。 (根据Xeo's comment)。对于 gcc 4.8 和 clang,它似乎已经可用。

boost::future<std::string> f2 = f1.then([](boost::future<int>& f)->std::string {
                                                             ^

【讨论】:

  • 使用 boost 1.55 它需要一个右值引用,即 future && f
猜你喜欢
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2016-12-04
  • 1970-01-01
相关资源
最近更新 更多