c++ - std::unordered_map 包装在类中时不接受 std::thread

标签 c++ multithreading templates unordered-map

如果我尝试保存 int和一个 std::threadstd::unordered_map<int, std::thread>一切似乎都运行良好。

但是如果我把 std::unordered_map 包装起来,然后我在标准库中收到错误。

错误是:no matching constructor for initialization of '_Mypair'

有没有办法解决这个问题并使其在包装时正常工作?

这有效:

源.cpp

#include <thread>
#include <unordered_map>

void display() {}

int main()
{
    std::unordered_map<int, std::thread> map_;
    std::thread tempThread(&display);
    map_.emplace(0, std::move(tempThread));

    return 0;
}

这不起作用:

MapperWrapper.h

#ifndef MAPPERWRAPPER_H
#define MAPPERWRAPPER_H

#include <unordered_map>
#include <utility>

template <typename KeyType, typename valueType> class MapperWrapper
{
    public:
        void add(KeyType key, valueType value)
        {
            mapper.emplace(std::make_pair(key, value));
        }

    protected:
        std::unordered_map<KeyType, valueType> mapper;
};
#endif // MAPPERWRAPPER_H

源.cpp

#include <thread>
#include "MapperWrapper.h"

void display() {}

int main()
{
    MapperWrapper<int, std::thread> map_;
    std::thread tempThread(&display);
    map_.add(0, std::move(tempThread));

    return 0;
}

这会产生错误:

||=== Build: Debug in Testing (compiler: LLVM Clang Compiler) ===|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|327|error: no matching constructor for initialization of '_Mypair' (aka 'pair<int, std::thread>')|
C:\Users\usr\Desktop\Project2\Project2\MapperWrapper.h|12|in instantiation of function template specialization 'std::make_pair<int &, std::thread &>' requested here|
C:\Users\usr\Desktop\Project2\Project2\Source.cpp|12|in instantiation of member function 'MapperWrapper<int, std::thread>::add' requested here|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|96|note: candidate template ignored: requirement 'is_copy_constructible<thread>::value' was not satisfied [with _Uty1 = int, _Uty2 = std::thread]|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|107|note: candidate template ignored: requirement 'is_copy_constructible<thread>::value' was not satisfied [with _Uty1 = int, _Uty2 = std::thread]|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|167|note: candidate template ignored: requirement 'is_constructible<thread, thread &>::value' was not satisfied [with _Other1 = int &, _Other2 = std::thread &]|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|181|note: candidate template ignored: requirement 'is_constructible<thread, thread &>::value' was not satisfied [with _Other1 = int &, _Other2 = std::thread &]|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|85|note: candidate constructor template not viable: requires 0 arguments, but 2 were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|121|note: candidate constructor template not viable: requires single argument '_Right', but 2 arguments were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|132|note: candidate constructor template not viable: requires single argument '_Right', but 2 arguments were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|150|note: candidate constructor template not viable: requires 4 arguments, but 2 were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|157|note: candidate constructor template not viable: requires 3 arguments, but 2 were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|195|note: candidate constructor template not viable: requires single argument '_Right', but 2 arguments were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|209|note: candidate constructor template not viable: requires single argument '_Right', but 2 arguments were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|112|note: candidate constructor not viable: requires 1 argument, but 2 were provided|
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility|113|note: candidate constructor not viable: requires 1 argument, but 2 were provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

最佳答案

您已经按值接受值类型。由于函数参数是本地的,因此您可以移动它(也可以移动键)

void add(KeyType key, valueType value)
{
    mapper.emplace(std::move(key), std::move(value));
}

也无需使用 std::make_pair 来放置到 map 中。

如果你想更复杂一点,你可以在add中添加一些完善的转发支持,以避免创建多余的中间对象。

template<typename... Args>
void add(Args&&... args)
{
    mapper.emplace(std::forward<Args>(args)...);
}

关于c++ - std::unordered_map 包装在类中时不接受 std::thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48084801/

相关文章:

c++ - 派生类中的模板方法优于非模板方法

c++ - 传递 `boost::variant` 数据类型的优雅方法

python - Django 数据库和线程

javascript - 使用js脚本生成<head>和&lt;footer&gt;

c++ - 使用constexpr返回函数值C++的模板函数调用

c++ - 如何计算C++模板函数中有效指针元素的总数?

c++ - C/C++ 用 ascii 艺术声明 2D/3D 数组

c++ - 关于非重复 rand 函数

c# - Object Disposed异常与多线程应用

java - 搜索方法返回的超时时间,否则会抛出超时消息