The following code does not compile on Linux x86_64:
main.cpp
#include <mongo/client/dbclient.h> using namespace mongo; int main() { int64_t value = 1; BSONObj obj = BSON("value" << value); }
Output:
In file included from /usr/include/mongo/client/../util/../util/../db/jsobj.h:43:0, from /usr/include/mongo/client/../util/../util/sock.h:25, from /usr/include/mongo/client/../util/message.h:20, from /usr/include/mongo/client/dbclient.h:21, from main.cpp:1: /usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h: In member function 'mongo::BSONObjBuilder& mongo::BSONObjBuilderValueStream::operator<<(T) [with T = long int]': main.cpp:7:19: instantiated from here /usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h:165:9: error: call of overloaded 'append(const char*&, long int&)' is ambiguous /usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h:165:9: note: candidates are: /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:188:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, bool) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:196:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, int) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:204:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, unsigned int) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:209:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, long long int) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:265:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, double) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:345:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, mongo::Date_t) /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:378:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, const char*) <near match> /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:378:25: note: no known conversion for argument 2 from 'long int' to 'const char*' /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:382:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, const string&) <near match> /usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:382:25: note: no known conversion for argument 2 from 'long int' to 'const string& {aka const std::basic_string<char>&}'
The problem is that MongoDB is using long long for 64 bit values. On Linux x86_64 (LP64 data model) long is used for the int64_t and boost::int64_t types and this yields to the compilation error, where long long is not considered the same 64 bit signed type as long.
Possible solutions:
- Add the proper appender for the long type only for LP64 data model:
mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, long int);
- Avoid these implementation dependent data types and use exact data types from standard C99 integer types. MongoDB depends on Boost, so <boost/integer.hpp> could be used without any problem.
I would recommend the 2nd option, because it is a clean solution. Using int, long, etc. data types might generate several unexpected issues in the future, especially when MongoDB has to be ported to various architectures.