2013-10-11 6 views
2

boost :: future .then() 기능을 사용하려고합니다. 스 니펫에서 가져 Boost 1.54.0 thread synchronisation documentationboost :: future .then() 사용시 컴파일 오류

#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)); 
              }); 
} 

설정 :
우분투 13.04
g ++ 버전 g ++ (우분투 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’ 
              }); 

여기에 뭔가 빠졌는지 알려주세요.

+2

Boost.ResultOf는 기본적으로'decltype'을 사용하지 않으므로 lambdas에서 작동하지 않습니다. Boost 헤더를 포함하기 전에'#define BOOST_RESULT_OF_USE_DECLTYPE'을 정의하십시오. – Xeo

답변

4

미래에 .then()에 대한 참조를 전달하면 컴파일 문제가 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 { 
                  ^
+1

부스트가 1.55 인 경우 r 값 참조가 필요합니다. 즉, 미래 && f – Sumant