0

trying to compile old project that has many uses of shared_ptr with the vs2010. so, i have precompiled header (stdafx.h) with:

..
using namespace std; ..

#include "boost/shared_ptr"
using namespace boost;

and later in the code i intensively use shared_ptr spObject;

What should I change in the stdafx.h so that I will no need to replace everywhere in the code *shared_ptr* to *some_namespace::shared_ptr*?
Is it possible to do without namespace conflicts of boost/boost::tr1/std::tr1/std?

Now I have a lot of errors:

error C2872: 'shared_ptr' : ambiguous symbol could be 'k:\boost10\boost\smart_ptr\shared_ptr.hpp(165) : boost::shared_ptr' or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1418) : std::tr1::shared_ptr'

Sergey Leyko
  • 369
  • 6
  • 14
  • May be it is not possible? The only variant I see is to define the macro **#define shared_ptr std::shared_ptr** – Sergey Leyko Feb 19 '12 at 18:52
  • Now I think it is not possible in c++ (without explicitly qualifying the namespace), it's so sad :( – Sergey Leyko Feb 19 '12 at 19:07
  • 1
    This is one of the reasons that `using namespace` is bad. Don't use it in future projects. Or at the very least only use it on _one_ library. You can still avoid a lot of namespace qualifications by using more limited `using std::shared_ptr` etc. statements. – bames53 Feb 26 '12 at 07:32

1 Answers1

4

Don't put using namespace in headers, as you've discovered it can break headers that follow, and because you can't change those headers there isn't much you can do about it.

At function scope you can use a using declaration to disambiguate:

void f()
{
  using std::tr1::shared_ptr;
  shared_ptr<int> p;
}

But that won't work in the global namespace, because you've already polluted that scope with the careless using directives.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521