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!