big昧<< Mongodb C ++驱动程序文档生成器中的运算符 [英] ambiguous << operator in Mongodb C++ driver document builder

查看:75
本文介绍了big昧<< Mongodb C ++驱动程序文档生成器中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用v3驱动程序来构建mongodb文档.我使用的是来自"char *"指针数组的字符串,但我不断收到表示<<的错误.运算符不明确.确切的错误是:

I am trying to build a mongodb document using the v3 driver. I am using strings from an array of "char *" pointers but I keep getting an error that says the << operator is ambiguous. The exact error is:

item.cpp:105:错误:运算符<<"(运算符类型为

item.cpp:105: error: ambiguous overload for ‘operator<<’ (operand types are

‘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是一个字符串数组,例如:

dbTypeString is an array of strings like:

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

mongo代码的简化版本如下

a simplified version of the mongo code looks like

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

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

奇怪的是:doc<< "string1"<< "string2"正常.

what is odd is that: doc << "string1" << "string2" works fine.

有什么建议吗?

推荐答案

此处的错误消息可能很难解析,但重要的部分是ambiguous overload for ‘operator<<’bsoncxx::v_noabi::builder::stream::key_context<>. <<运算符对于类重载. bsoncxx::builder::stream::key_context (内部是处理文档生成器的追加内容的工具),并且编译器不知道使用哪个定义,因为有多个定义可以使用.更具体地说,没有operator<<接受const char *自变量的定义,但是有接受std::stringstdx::string_view的定义,并且const char *可以强制为这两种类型中的任何一种.

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

要编译代码,您有两个选择.首先,您只需将dbTypeString更改为包含std::string而不是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 *:

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

或者,如果您希望将数组保留为当前类型,则可以手动将const char *强制转换为std::string,这消除了调用哪个重载的歧义:

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:

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

稍微切线的是,通常使用流构建器比较棘手(特别是关于<<运算符的返回类型,这在过去引起了相当大的混乱).我个人建议使用基本生成器(如果可能);尽管该API的工效学性稍差一些,但它并没有流构建器那样复杂,这意味着(在非常罕见的情况下)您会因使用不当而导致错误,因此可能会调试问题更直接.

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.

这篇关于big昧&lt;&lt; Mongodb C ++驱动程序文档生成器中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆