[CXX-44] Client facing headers should not say "using namespace std" in header scope Created: 21/Jun/10  Updated: 04/May/14  Resolved: 17/Mar/14

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: legacy-0.8.0

Type: Improvement Priority: Critical - P2
Reporter: Andrew Morrow (Inactive) Assignee: Andrew Morrow (Inactive)
Resolution: Done Votes: 3
Labels: legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All



 Description   

mongo/bson/bsontypes.h does 'using namespace std' in header context.

Even though its contained in the mongo namespace, best practice is that names from other namespaces should be namespace qualified in headers, for the following reason:

// From who knows where in my include chain...
class string

{ /* ... */ }

; // NOT std::string, global scope

// From mongo headers...
#include <string>

namespace mongo {
using namespace std;
class BSONObjBuilder {
// ...
// ERROR: 'string' ambiguous here!!!
BSONObjBuilder& append(const string& fieldName , BSONObj subObj);
// ...
} // namespace mongo

By 'using namespace std', and using names from the std namespace without qualification, you expose yourself to ambiguity with any global names introduced in other headers that shadow names in namespace std. When integrating the BSON headers (or other client facing headers) into complex existing applications that already integrate many libraries the potential for such conflicts materializing in practice grows considerably.



 Comments   
Comment by Githook User [ 17/Mar/14 ]

Author:

{u'username': u'acmorrow', u'name': u'Andrew Morrow', u'email': u'acm@mongodb.com'}

Message: CXX-65 CXX-44 Remove pre-compiled header and using namespace std
Branch: legacy
https://github.com/mongodb/mongo-cxx-driver/commit/85613ca750171db4b7e1c2fd64f45ba101dcf126

Comment by Andrew Morrow (Inactive) [ 13/Mar/14 ]

https://github.com/mongodb/mongo-cxx-driver/pull/20

Comment by Matt Kangas [ 10/Feb/14 ]

Note, https://github.com/mongodb/mongo/pull/8 contains a Perl script to automate the conversion, plus a clientNameSpaceTest.cpp and globalNameSpacePoisoner.h to smoke out edge cases. (Pull request closed as "wontfix" in server codebase)

Comment by auto [ 06/Mar/12 ]

Author:

{u'login': u'milkie', u'email': u'milkie@10gen.com', u'name': u'Eric Milkie'}

Message: SERVER-1268: don't put "using namespace" statements in headers
Branch: master
https://github.com/mongodb/mongo/commit/fd713371498dec2b2cb319c3a50c7a0c49a062ec

Comment by Dave Spratley [ 11/Feb/12 ]

Putting the 'use namespace std;' into the mongodb namespace is better, but as you probably know, not a complete fix - 3rd party libraries' symbols will still clash with Mongo's if you include them before Mongo headers. Of course the fix is to include them after the Mongo headers.

Comment by auto [ 27/Jul/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: in headers at least put using namespace in mongo namespace SERVER-1268
Branch: master
https://github.com/mongodb/mongo/commit/8f27050318e29fe960326979a959966dc7a7b0ad

Comment by Andrew Morrow (Inactive) [ 13/Jul/11 ]

I am also frustrated that this has been ignored for so long, especially given how easy it is to fix and how damaging it is to client code.

Comment by John Zwinck [ 13/Jul/11 ]

I just got burned by a really bad case of this. In bsonobjbuilder.h (http://api.mongodb.org/cplusplus/1.9.0/bsonobjbuilder_8h_source.html) it says "using namespace std;" at file scope (!!). This is a big, huge no-no in C++, and this particular instance took two people half an hour to track down. I'm not entirely sure how we'll fix it. Make our own dev package which patches it? Crazy preprocessor hackery? Stop using MongoDB?

It's been over a year. And eight months since a fix was submitted (thanks Jeff). Please fix this now.

Comment by Jeff Donner [ 05/Nov/10 ]

Hi, I posted a pull request for patches for this at:

https://github.com/mongodb/mongo/pull/9

These aren't actually patches, but rather a test for 1268, and a script to automate the busy work of fixing it. I can do an actual patch if you'd like but, because it touches so many headers and symbols I expect it'd go stale quickly. Details are in the patches themselves, especially in the top of 1268.pl.

edit - a lot of whitespace diffs in SConstruct - the only meaningful one is the line 1225:
+clientTests += [ clientEnv.Program( "clientNamespaceTest" , [ "client/examples/clientNamespaceTest.cpp" ] ) ]

Generated at Wed Feb 07 21:58:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.