带有多个参数的Map.emplace C ++ 17

时间:2018-05-02 14:44:18

标签: c++ dictionary

使用C ++ 17:有

using working_t = bool;
std::map<std::thread, working_t> _pool;

我正在尝试向此地图添加一个新线程,但我找不到合适的语法..

class ThreadPool {
   std::map<std::thread, working_t> _pool;

   void init() {
      _pool.emplace(&ThreadPool::thread_init, this, false);
   }
   void thread_init();
};

这应该添加一个线程到地图,并将false作为值,但它无法编译..这可能吗?

1 个答案:

答案 0 :(得分:3)

你需要:

_pool.emplace(
    std::piecewise_construct,
    std::forward_as_tuple(&ThreadPool::thread_init, this),
    std::forward_as_tuple(false)
);

...区分键和值的参数列表。但是,您会遇到std::thread没有operator <的问题,但这会引发另一个问题:)