首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当std::unordered_map被包装在类中时,它不接受std::线程

当std::unordered_map被包装在类中时,它不接受std::线程
EN

Stack Overflow用户
提问于 2018-01-03 20:39:57
回答 1查看 344关注 0票数 2

如果我试图将intstd::thread保存在std::unordered_map<int, std::thread>中,一切似乎都很好。

但是如果我包装了std::unordered_map,那么在标准库中就会出现一个错误。

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

有什么方法可以绕过这个问题,让它在包装的时候工作吗?

这样做是可行的:

Source.cpp

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

Source.cpp

代码语言:javascript
运行
复制
#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;
}

这将生成错误:

代码语言:javascript
运行
复制
||=== 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)) ===|
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-03 20:45:21

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

代码语言:javascript
运行
复制
void add(KeyType key, valueType value)
{
    mapper.emplace(std::move(key), std::move(value));
}

也没有必要使用std::make_pair将其嵌入到地图中。

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

代码语言:javascript
运行
复制
template<typename... Args>
void add(Args&&... args)
{
    mapper.emplace(std::forward<Args>(args)...);
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48084801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档