【问题标题】:error C2660: 'std::pair<a,b>::pair': function does not take 2 argumentserror C2660: \'std::pair<a,b>::pair\': 函数不接受 2 个参数
【发布时间】: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_mutexqu_cv 发表评论,那么我就可以安放了。错误与这两个成员有什么关系?默认构造函数不是初始化 struct 成员的情况吗? 我知道编译器删除了复制/赋值/移动构造函数。

【问题讨论】:

  • std::mutex 不可复制。
  • @ChrisMM 嗯,std::condition_variable 也是如此,这完全有道理,但错误消息是如此具有误导性。它可能回答了我的问题。谢谢。但我仍然有兴趣知道为什么编译器会发出这样的消息。
  • 在日志的某个更深的地方,它应该说互斥体是不可复制的。
  • @MarekR 不幸的是,我无法在视觉工作室发出的日志中找到它。

标签: c++ queue ordered-map


【解决方案1】:

无论如何要解决这个问题,您需要自定义复制构造函数和赋值运算符。 互斥体还建议在所有场景中对qu进行一些同步,因此所有字段都应该是私有的(所以struct应该更改为class)。

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

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多