无法将元素添加到 bsoncxx 文档
Can't add element to bsoncxx document
无法将元素添加到 bsoncxx 文档
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd";
const char* value = "123";
doc.append(kvp(key, value));
bsoncxx::v_noabi::builder::basic::sub_document::append_(bsoncxx::v_noabi::builder::concatenate_doc)': cannot convert argument 1 from '_Ty' to 'bsoncxx::v_noabi::builder::concatenate_doc'
1> with
1> [
1> _Ty=std::tuple<const char *&,const char *&>
1> ]
但此代码有效
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd";
const char* value = "123";
doc.append(kvp("asd", value));
mongo cxx 驱动 v3.3.1
您的初始代码不起作用,因为 sub_document::append_
没有针对 const char*
的专门化(模板只有 enabled for std::string and string_view)。
你的第二个例子确实有效,因为有一个 for string literals.
错误跟踪的以下部分提供了更多信息:
1> c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\builder\basic\sub_document.hpp(46):
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\c++\workspace\test.cpp(207):
note: see reference to function template instantiation 'void bsoncxx::v_noabi::builder::basic::sub_document::append<std::tuple<const char *&,const char *&>,>(Arg &&)' being compiled
要使其正常工作,您只需将其作为 std::string 传递(最好直接将密钥构建为 std::string):
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd"; // or: std::string keyStr("asd");
const char* value = "123";
doc.append(kvp(std::string(key), value)); // or: doc.append(kvp(keyStr,value));
如果你真的想使用 const char*
你可以在 bsoncxx/builder/basic/sub_document.hpp
:
添加 append_
的特化
template <typename K, typename V>
BSONCXX_INLINE typename std::enable_if<
std::is_same<typename std::decay<K>::type, const char *>::value>::type
append_(std::tuple<K, V>&& t) {
_core->key_owned(std::forward<K>(std::get<0>(t)));
impl::value_append(_core, std::forward<V>(std::get<1>(t)));
}
希望对您有所帮助!
这段代码有效
#include <bsoncxx/stdx/string_view.hpp>
auto doc = bsoncxx::builder::basic::document{};
bsoncxx::stdx::string_view key = "asd";
const char* value = "123";
doc.append(kvp(key, value));
无法将元素添加到 bsoncxx 文档
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd";
const char* value = "123";
doc.append(kvp(key, value));
bsoncxx::v_noabi::builder::basic::sub_document::append_(bsoncxx::v_noabi::builder::concatenate_doc)': cannot convert argument 1 from '_Ty' to 'bsoncxx::v_noabi::builder::concatenate_doc'
1> with
1> [
1> _Ty=std::tuple<const char *&,const char *&>
1> ]
但此代码有效
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd";
const char* value = "123";
doc.append(kvp("asd", value));
mongo cxx 驱动 v3.3.1
您的初始代码不起作用,因为 sub_document::append_
没有针对 const char*
的专门化(模板只有 enabled for std::string and string_view)。
你的第二个例子确实有效,因为有一个 for string literals.
错误跟踪的以下部分提供了更多信息:
1> c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\builder\basic\sub_document.hpp(46):
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\c++\workspace\test.cpp(207):
note: see reference to function template instantiation 'void bsoncxx::v_noabi::builder::basic::sub_document::append<std::tuple<const char *&,const char *&>,>(Arg &&)' being compiled
要使其正常工作,您只需将其作为 std::string 传递(最好直接将密钥构建为 std::string):
auto doc = bsoncxx::builder::basic::document{};
const char* key = "asd"; // or: std::string keyStr("asd");
const char* value = "123";
doc.append(kvp(std::string(key), value)); // or: doc.append(kvp(keyStr,value));
如果你真的想使用 const char*
你可以在 bsoncxx/builder/basic/sub_document.hpp
:
append_
的特化
template <typename K, typename V>
BSONCXX_INLINE typename std::enable_if<
std::is_same<typename std::decay<K>::type, const char *>::value>::type
append_(std::tuple<K, V>&& t) {
_core->key_owned(std::forward<K>(std::get<0>(t)));
impl::value_append(_core, std::forward<V>(std::get<1>(t)));
}
希望对您有所帮助!
这段代码有效
#include <bsoncxx/stdx/string_view.hpp>
auto doc = bsoncxx::builder::basic::document{};
bsoncxx::stdx::string_view key = "asd";
const char* value = "123";
doc.append(kvp(key, value));