首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误C2660:'std::pair<a,b>::pair':function不使用2个参数

错误C2660:'std::pair<a,b>::pair':function不使用2个参数
EN

Stack Overflow用户
提问于 2022-11-23 16:20:26
回答 1查看 55关注 0票数 0

我正试图创建一个结构并插入一个地图,如下所示:

代码语言:javascript
复制
    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);

但我得到了以下错误:

代码语言:javascript
复制
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,嵌入构造元素内部。如果这是正确的,那么为什么编译器试图创建对嵌入呢?我发现编译器合成的对的语法很奇怪,这就是它抱怨的原因。但为什么会发生这种情况,我能做些什么来解决这个问题呢?

我试图显式地传递make_pair(),但这并没有帮助。

如果我评论qu_mutexqu_cv,那么我就可以做emplace了。错误与这两个成员有什么关系?不是默认的消耗者初始化struct成员的情况吗?我知道编译器删除了复制/赋值/移动构造函数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-23 18:53:18

无论如何,要解决这个问题,您需要自定义复制构造函数和赋值操作符。此外,互斥表示所有场景中qu的同步,因此所有字段都应该是私有的(因此struct应该改为class)。

代码语言:javascript
复制
class Queue_ctx {
    mutable std::mutex qu_mutex;
    std::condition_variable qu_cv;
    std::queue<std::vector<std::byte>> qu;

public:
    Queue_ctx() = default;
    Queue_ctx(const Queue_ctx& other)
        : Queue_ctx(other, std::scoped_lock{ other.qu_mutex })
    {
    }

    Queue_ctx(const Queue_ctx& other, const std::scoped_lock<std::mutex>&)
        : qu { other.qu }
    {
    }

    Queue_ctx(Queue_ctx&& other)
    : Queue_ctx(std::move(other), std::scoped_lock{ other.qu_mutex })
    {
    }

    Queue_ctx(Queue_ctx&& other, const std::scoped_lock<std::mutex>&)
        : qu { std::move(other.qu) }
    {
    }

    Queue_ctx& operator=(const Queue_ctx& other)
    {
        std::scoped_lock lock{ qu_mutex, other.qu_mutex };
        qu = other.qu;
        return *this;
    }

    Queue_ctx& operator=(Queue_ctx&& other)
    {
        std::scoped_lock lock{ qu_mutex, other.qu_mutex };
        qu = std::move(other.qu);
        return *this;
    }

    void push(const std::vector<std::byte>& v)
    {
        std::unique_lock lock{ qu_mutex };
        qu.push(v);
    }

    void push(std::vector<std::byte>&& v)
    {
        std::unique_lock lock{ qu_mutex };
        qu.push(std::move(v));
    }
};

https://godbolt.org/z/xn6orTedz

它可以编译,但还需要更多的测试。注意,缺少一些利用qu_cv的功能。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74550010

复制
相关文章

相似问题

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