我正在尝试创建一个结构并插入一个Map,如下所示:
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,emplace构造了元素inplace。如果这是真的,那么为什么编译器试图创建pair来进行emplace?我发现编译器合成的pair的语法很奇怪,这就是它抱怨的原因。但是为什么会发生这种情况,我该怎么做来解决这个问题?
我试图显式传递make_pair()
,但没有任何帮助。
如果我注解了qu_mutex
和qu_cv
,那么我就可以执行emplace。error和这两个成员有什么关系?不是默认consutructor初始化struct的成员的情况吗?我知道copy/assignment/move构造函数被编译器删除了。
1条答案
按热度按时间kzmpq1sx1#
无论如何,要解决这个问题,你需要定制复制构造函数和赋值运算符。另外,互斥体建议在所有场景中对
qu
进行一些同步,所以所有字段都应该是私有的(所以struct
应该改为class
)。https://godbolt.org/z/xn6orTedz
它可以编译,但是需要更多的测试。注意,使用
qu_cv
缺少一些功能。