8

I'm trying to port C++11 std::thread code to VC9 (VS 2008) using boost::thread. The 'equivalent' C++11 code below compiles fine on msvc12:

#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <cassert>

void thFun(int i) 
{
    std::cout << "Hello from thread " << i << " !\n";
}
int main()
{
    std::vector<std::thread> workers;
    for (int i = 0; i < 10; ++i)
    {
        auto th = std::thread(&thFun, i);
        workers.push_back(std::move(th));
        assert(!th.joinable());
    }
    std::cout << "Hello from main!\n";
    std::for_each(workers.begin(), workers.end(), [](std::thread& th)
    {
        assert(th.joinable());
        th.join(); 
    });
    return 0;
}    

I'd like to port the code to C++03 using the msvc9 compiler and Boost 1.55. How do I resolve the following compile error:

#include <iostream>
#include <boost/thread.hpp>
#include <cassert>
#include <boost/foreach.hpp>
#include <boost/container/vector.hpp>

void thFun(int i) 
{
    std::cout << "Hello from thread " << i << " !\n";
}
void foo(boost::thread& th)
{
    assert(th.joinable());
    th.join(); 
}

int main()
{    
    boost::container::vector<boost::thread> workers;
            for (int i = 0; i < 10; ++i)
    {
        BOOST_AUTO(th, boost::thread(&thFun, i));
        workers.push_back(boost::move(th));
        assert(!th.joinable());
    }

    std::cout << "Hello from main!\n";
    BOOST_FOREACH(boost::thread& t, workers)
    {
        foo(t);
    }
    return 0;
}

Compile error is:

d:\program data\boost\boost_1_55_0\boost\preprocessor\iteration\detail\local.hpp(37): error C2770: invalid explicit template argument(s) for '::boost::move_detail::enable_if_c<boost::enable_move_utility_emulation<T>::value&&!boost::move_detail::is_rv<T>::value,const T&>::type boost::forward(const ::boost::move_detail::identity<T>::type &)'
          d:\program data\boost\boost_1_55_0\boost\move\utility.hpp(78) : see declaration of 'boost::forward'
          d:\program data\boost\boost_1_55_0\boost\container\vector.hpp(1797) : see reference to function template instantiation 'void boost::container::allocator_traits<Alloc>::construct<T,T>(Alloc &,T *,const P0 &)' being compiled
          with
          [
              Alloc=std::allocator<boost::thread>,
              T=boost::thread,
              P0=boost::thread
          ]
          d:\program data\boost\boost_1_55_0\boost\container\vector.hpp(1791) : while compiling class template member function 'void boost::container::vector<T>::priv_push_back(const T &)'
          with
          [
              T=boost::thread
          ]
          d:\work\boostthreadscratch\boostthreadscratch\boostthreadscratch.cpp(19) : see reference to class template instantiation 'boost::container::vector<T>' being compiled
          with
          [
              T=boost::thread
          ]
6
  • Is that the full error message? In what line of your source does the error appear?
    – Albert
    Nov 14, 2013 at 12:08
  • This might be related. Have you tried to replace BOOST_AUTO?
    – Albert
    Nov 14, 2013 at 12:30
  • 1
    Before including any boost headers, try this: #define BOOST_THREAD_USES_MOVE. See here for reference.
    – Albert
    Nov 14, 2013 at 12:35
  • @Albert Full message edited in. Error appears related to line 19 boost::container::vector<boost::thread> workers but the actual error is on line 23 workers.push_back(boost::move(th));
    – hhbilly
    Nov 14, 2013 at 17:21
  • 1
    using #define BOOST_THREAD_USES_MOVE works. Compiling in vc9 correctly now.
    – hhbilly
    Nov 14, 2013 at 17:28

1 Answer 1

4

Put this at the beginning:

#define BOOST_THREAD_USES_MOVE

See here for reference. It enables the emulation provided by Boost.Move (boost::move) for boost::thread, which is disabled by default in Boost.Thread version 2 (which might be used by MSVC9 in your case).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.