【发布时间】:2022-11-24 03:05:05
【问题描述】:
我正在尝试创建一个结构并插入一个地图,如下所示:
struct Queue_ctx {
std::mutex qu_mutex;
std::condition_variable qu_cv;
std::queue<std::vector<std::byte>> qu;
};
std::map<std::string, Queue_ctx> incoming_q_map;
Queue_ctx qctx;
std::vector<std::byte> vect(100);
qctx.qu.push(vect);
incoming_q_map.emplace("actor", qctx);
但我收到以下错误:
error C2660: 'std::pair<const std::string,main::Queue_ctx>::pair': function does not take 2 arguments
message : see declaration of 'std::pair<const std::string,main::Queue_ctx>::pair'
message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,const char(&)[6],main::Queue_ctx&>(_Alloc &,_Objty *const ,const char (&)[6],main::Queue_ctx &)' being compiled
with
[
_Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,main::Queue_ctx>,std::_Default_allocator_traits<std::allocator<std::pair<const std::string,main::Queue_ctx>>>::void_pointer>>,
_Ty=std::pair<const std::string,main::Queue_ctx>,
_Objty=std::pair<const std::string,main::Queue_ctx>
]
AFAIU,就地构造元素。如果这是真的那么为什么编译器试图创建对来放置? 我看到编译器合成的 pair 语法很奇怪,这就是它抱怨的原因。但为什么会发生这种情况,我该怎么做才能解决这个问题?
我试图明确传递make_pair(),但这没有帮助。
如果我对 qu_mutex 和 qu_cv 发表评论,那么我就可以安放了。错误与这两个成员有什么关系?默认构造函数不是初始化 struct 成员的情况吗?
我知道编译器删除了复制/赋值/移动构造函数。
【问题讨论】:
-
std::mutex不可复制。 -
@ChrisMM 嗯,
std::condition_variable也是如此,这完全有道理,但错误消息是如此具有误导性。它可能回答了我的问题。谢谢。但我仍然有兴趣知道为什么编译器会发出这样的消息。 -
在日志的某个更深的地方,它应该说互斥体是不可复制的。
-
@MarekR 不幸的是,我无法在视觉工作室发出的日志中找到它。
标签: c++ queue ordered-map