【问题标题】:Porting C++11 std::thread to boost::thread compile issues将 C++11 std::thread 移植到 boost::thread 编译问题
【发布时间】:2013-11-26 07:29:48
【问题描述】:

我正在尝试使用 boost::thread 将 C++11 std::thread 代码移植到 VC9 (VS 2008)。下面的“等效”C++11 代码在 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;
}    

我想使用 msvc9 编译器和 Boost 1.55 将代码移植到 C++03。如何解决以下编译错误:

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

编译错误是:

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
          ]

【问题讨论】:

  • 这是完整的错误信息吗?错误出现在源代码的哪一行?
  • This 可能是相关的。你试过替换BOOST_AUTO吗?
  • 在包含任何增强标头之前,请尝试以下操作:#define BOOST_THREAD_USES_MOVE。参考here
  • @Albert 已编辑完整消息。出现与第 19 行 boost::container::vector&lt;boost::thread&gt; workers 相关的错误,但实际错误在第 23 行 workers.push_back(boost::move(th));
  • 使用#define BOOST_THREAD_USES_MOVE 有效。现在可以在 vc9 中正确编译了。

标签: c++ visual-c++ boost boost-thread boost-move


【解决方案1】:

把这个放在开头:

#define BOOST_THREAD_USES_MOVE

请参阅here 以供参考。它启用了 Boost.Move (boost::move) 为 boost::thread 提供的模拟,在 Boost.Thread 版本 2 中默认禁用(在您的情况下可能由 MSVC9 使用)。

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2012-09-25
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多