英文:
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("Name", "Smith") );
and
auto those_guys = bsoncxx::builder::basic::document{};
those_guys.append( kvp("Name", "Jones") );
and then based on some parameters
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 here
}
else if (those)
{
full_query = those_guys; // <--- E1776 here
}
Using Visual Studio 2017, I'm getting the error
Error (active) E1776 function "bsoncxx::v_noabi::builder::basic::document::operator=(const bsoncxx::v_noabi::builder::basic::document &) throw()" (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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论