-1

Can't add an element to bsoncxx document

    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>          ]

but this code is work

    auto doc = bsoncxx::builder::basic::document{};

    const char* key = "asd";
    const char* value = "123";

    doc.append(kvp("asd", value));

mongo cxx driver v3.3.1

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Alex
  • 7
  • 2

2 Answers2

0

Your initial code does not work because there's no specialization of sub_document::append_ for const char* (the template is only enabled for std::string and string_view).

Your second example does work because there's one for string literals.

The following part of the error trace gives a bit more information:

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

To make it work you can just pass it as a std::string (so better to directly build the key as 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));

If you really want to use a const char* you can add the specialization of append_ at bsoncxx/builder/basic/sub_document.hpp:

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)));
}

I hope it helps!

SPM
  • 405
  • 1
  • 5
  • 16
  • yes with std :: string as a parameter, this code is compiled, but an error occurs at runtime in xutility.h line 222 `*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)` **_Pnext** was 0x2B600647369. – Alex Apr 28 '19 at 05:02
  • Is that file (xutility.h) part of mongocxx driver? I believe it is now a different issue. Please try to search in StackOverflow if your current issue (or a similar one) has been already asked in the past, and if not, you'd need to open a different question for that with the correct labels and with the code that is generating that issue. – SPM May 01 '19 at 15:16
-1

This code is work

#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));
Alex
  • 7
  • 2