[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... ; // NOT std::string, global scope // From mongo headers... 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: |
| Comment by Andrew Morrow (Inactive) [ 13/Mar/14 ] |
| 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: |
| 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 |
| 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: |