为什么将一个bsoncxx::document分配给另一个bsoncxx::document时会出现错误?

huangapple go评论69阅读模式
英文:

Why am I getting an error when assigning a bsoncxx::document to another bsoncxx::document?

问题

使用不同选项,我尝试使用bsoncxx::builder::basic::document对象构建复合查询。因此,我可以构建以下代码:

auto these_guys = bsoncxx::builder::basic::document{};
these_guys.append( kvp("Name", "Smith") );

auto those_guys = bsoncxx::builder::basic::document{};
those_guys.append( kvp("Name", "Jones") );

然后基于一些参数,创建完整的查询:

auto full_query = bsoncxx::builder::basic::document{};
if (these && those)
{
    full_query.append(kvp("$and", make_array(these_guys, those_guys));
}
else if (these)
{
    full_query = these_guys;   // <--- 这里出现了E1776错误
} 
else if (those)
{
    full_query = those_guys;   // <--- 这里出现了E1776错误
}

在使用Visual Studio 2017时,我遇到了以下错误:

Error (active)	E1776	function "bsoncxx::v_noabi::builder::basic::document::operator=(const bsoncxx::v_noabi::builder::basic::document &)"(隐式声明)不能被引用 -- 它是一个已删除的函数

根据文档,我理解我应该能够执行这个赋值操作。请纠正我的理解。

英文:

Using various options, I'm attempting to build a compound query using bsoncxx::builder::basic::document objects. So I can build

auto these_guys = bsoncxx::builder::basic::document{};
these_guys.append( kvp(&quot;Name&quot;, &quot;Smith&quot;) );

and

auto those_guys = bsoncxx::builder::basic::document{};
those_guys.append( kvp(&quot;Name&quot;, &quot;Jones&quot;) );

and then based on some parameters

auto full_query = bsoncxx::builder::basic::document{};
if (these &amp;&amp; those)
{
    full_query.append(kvp(&quot;$and&quot;, make_array(these_guys , those_guys)));
}
else if (these)
{
    full_query = these_guys;   // &lt;--- E1776 here
} 
else if (those)
{
    full_query = those_guys;   // &lt;--- E1776 here
}

Using Visual Studio 2017, I'm getting the error

Error (active)	E1776	function &quot;bsoncxx::v_noabi::builder::basic::document::operator=(const bsoncxx::v_noabi::builder::basic::document &amp;) throw()&quot; (declared implicitly) cannot be referenced -- it is a deleted function

It's my understanding from the documentation that I should be able to make this assignment. Please correct my thinking.

答案1

得分: 1

文档中:

document & operator= (document &&doc) noexcept

移动赋值运算符。

没有提到复制赋值运算符。因为已经定义了移动赋值运算符,复制赋值运算符会被自动删除。

所以你必须使用移动赋值:

full_query = std::move(these_guys);
英文:

From the documentation:

> document & operator= (document &&doc) noexcept
>
> Move assignment operator.

There's no mention of a copy assignment operator. Since a move assignment operator was defined the copy assignment operator is automatically deleted.

So you have to use move assignment:

full_query = std::move(these_guys);

huangapple
  • 本文由 发表于 2023年3月4日 00:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定