10

Possible Duplicate:
Why is ‘using namespace std;’ considered a bad practice in C++?

I've used stl's shared_ptr many places in my code and I have used the following using statement anywhere that I have used shared_ptr:

using namespace std::tr1; 

Now I need to use boost::bimap. So I have to include the following header file in my code:

#include <boost/bimap.hpp> 

As soon as I include the bimap header file, the shared_ptr type becomes ambiguous, and I have to change all usages of shared_ptr to std::tr1::shared_ptr. Since this makes my code ugly, I am looking for a way to avoid this ambiguity without needing to declare shared_ptr everywhere with a fully qualified name. I was thinking of using typedef for std::tr1::shared_ptr, but maybe there are better ways too. Any advice would be appreciated!

2
  • 1
    So you have a using declaration for boost as well? Commented Dec 9, 2012 at 12:48
  • 6
    And we finally see why using directives are a scourge upon the nation. Just. Say. No. Commented Dec 9, 2012 at 12:49

2 Answers 2

7

simply do not introduce it. avoid the general using namespace ...

Sign up to request clarification or add additional context in comments.

Comments

5

How about the following:

using shared_ptr = std::tr1::shared_ptr; 

However, the correct answer would actual be to not use the using namespace ... statement, or only use it to create namespace aliases.

By the way, you know that std::tr1 is obselete since C++11 became standardised last year?

7 Comments

I know but due to some limitations, I cannot switch to C++11 yet.
and the above using does not work: error: expected nested-name-specifier before ‘share_ptr’ error: expected ‘;’ before ‘=’ token
@Meysam What compiler and what version of it are you using? It may be that it doesn't support the new type aliases yet.
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Template aliases are not supported until g++ 4.7 IIRC.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.