简体   繁体   English

Mongodb C ++驱动程序文档生成器中的不明确<<操作符

[英]ambiguous << operator in Mongodb C++ driver document builder

I am trying to build a mongodb document using the v3 driver. 我正在尝试使用v3驱动程序来构建mongodb文档。 I am using strings from an array of "char *" pointers but I keep getting an error that says the << operator is ambiguous. 我使用的是来自“ char *”指针数组的字符串,但始终收到表示<<操作符不明确的错误。 The exact error is: 确切的错误是:

item.cpp:105: error: ambiguous overload for 'operator<<' (operand types are item.cpp:105:错误:“ operator <<”的模棱两可的重载(操作数类型为

‘std::enable_if<true, bsoncxx::v_noabi::builder::stream::key_context<> >::type {aka bsoncxx::v_noabi::builder::stream::key_context<>}’ and ‘const char*’)
       << dbTypeString[dbType::IT_TYPE]

dbTypeString is an array of strings like: dbTypeString是一个字符串数组,例如:

const char * dbTypeString[] = {"string a", "string b"}

a simplified version of the mongo code looks like mongo代码的简化版本看起来像

bsoncxx::builder::stream::document doc{};

doc << dbTypeString[0] << "value string";

what is odd is that: doc << "string1" << "string2" works fine. 奇怪的是:doc <<“ string1” <<“ string2”正常工作。

Any suggestions? 有什么建议么?

The error message here can be a little hard to parse, but the important parts are ambiguous overload for 'operator<<' and bsoncxx::v_noabi::builder::stream::key_context<> . 这里的错误消息可能很难解析,但是重要的部分是ambiguous overload for 'operator<<'bsoncxx::v_noabi::builder::stream::key_context<> ambiguous overload for 'operator<<' The << operator is overloaded for the class bsoncxx::builder::stream::key_context (which is internally what is handling the appends to the document builder), and the compiler doesn't know which definition to use since there are multiple that could work. 对于类bsoncxx::builder::stream::key_context (在内部处理文档生成器的追加内容), <<操作符已重载,并且编译器不知道要使用哪个定义,因为存在多个可以工作。 More specifically, there is no definition of operator<< accepting a const char * argument, but there are definitions accepting std::string and stdx::string_view , and const char * could be coerced into either of those types. 更具体地说,没有定义operator<<接受const char *参数的定义,但是存在接受std::stringstdx::string_view ,并且const char *可以强制为这两种类型。

To get the code to compile, you have a couple of options. 要编译代码,您有两个选择。 First, you can just change dbTypeString to contain std::string instead of const char * : 首先,您可以将dbTypeString更改为包含std::string而不是const char *

std::string dbTypeString[] = {"string a", "string b"}

Alternately, if you would rather keep the array as its current type, you can manually cast the const char * to a std::string , which removes the ambiguity of which overload to call: 或者,如果您希望将数组保留为当前类型,则可以手动将const char * std::string转换为std::string ,从而消除了要调用的重载的歧义:

doc << static_cast<std::string>(dbTypeString[0]) << "value string";

On a slightly tangential note, the stream builder in general can be tricky to use (especially with regards to the return type of the << operator, which has caused a fair amount of confusion in the past). 稍微切线的是,通常使用流构建器比较棘手(尤其是关于<<操作符的返回类型,这在过去引起了相当大的混乱)。 I'd personally recommend using the basic builder if possible; 我个人建议尽可能使用基本构建器 while the API is a little less ergonomic, it doesn't suffer from the same complexity that the stream builder does, which means that in the (much rarer) occasions that you do get an error from using it improperly, debugging the issues will likely be more straightforward. 尽管该API的工效性稍差一些,但它并没有流构建器那样复杂,这意味着(在非常罕见的情况下)您会因使用不当而导致错误,因此可能会调试问题更直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM