首页 > 解决方案 > 带有多个参数的 Map.emplace C++ 17

问题描述

使用 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 作为值,但它无法编译.. 这可能吗?

标签: c++dictionary

解决方案


你需要:

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

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


推荐阅读