17

What is the difference between these two usage of using keyword:

using boost::shared_ptr;

and

using namespace boost;
John Carter
  • 53,924
  • 26
  • 111
  • 144
Bitmap
  • 12,402
  • 16
  • 64
  • 91

4 Answers4

35
using boost::shared_ptr;

Includes only the shared_ptr from the boost namespace in your current namespace. This means you can use the shared_ptr without qualifying it with namespace boost.

It is called a using declaration.


using namespace boost;

Includes all the symbols in the boost namespace in your current scope. This means you can use all the symbols in the boostnamespace without qualifying them with namespace boost.

It is called as using directive.


Why should you always prefer using declaration over using directive?

It is always better to use the first(using declaration) and avoid the second(using directive) because the second causes namespace pollution by bringing in potentially huge numbers of names in to the current namespace, many of which are unnecessary. The presence of the unnecessary names greatly increases the possibility of unintended name conflicts.

To quote Herb Sutter on the usage of using directive:

I find it helpful to think of a using directive as a marauding army of crazed barbarians that sows indiscriminate destruction wherever it passes--something that by its mere presence can cause unintended conflicts, even when you think you're allied with it.

FruitBreak
  • 570
  • 1
  • 7
  • 19
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • "It is better to use the first." Not necessarily. – David Heffernan Aug 07 '11 at 13:51
  • 2
    Yes, necessarily, as it significantly reduces the namespace pollution. – Puppy Aug 07 '11 at 13:54
  • @David Heffernan: "Not necessarily" unless you want to cause namespace pollution. – Alok Save Aug 07 '11 at 13:57
  • 3
    @DeadMG `using namespace` is widely used in standard library header files for example. In that situation the pollution is intentional. – David Heffernan Aug 07 '11 at 14:11
  • 3
    @David: a quick search of the stantard headers in gcc 4.2 found that only `debug/debug.h` (which is an implementation detail) employs the `using namespace` directive, on the other hand, the `using` declaration is used over 1000 times. – David Rodríguez - dribeas Aug 07 '11 at 14:23
  • Actually, have I remembered it the wrong way round? Perhaps what I'm thinking of is how newer C++ headers do things like `namespace xxx { #include }`. Sorry. – David Heffernan Aug 07 '11 at 14:28
  • 1
    @Als: the answer is missing some details, I am feeling too lazy to go and find all of them and check in the standard, but you can consider the case of implementing `swap` as a member function in a class: `struct X { int d; void swap( X& other ) { using std::swap; swap( d, other.d ); }` is correct, while replacing the *using declaration* with a *using directive* will fail to compile. – David Rodríguez - dribeas Aug 07 '11 at 14:28
  • 2
    @David Heffernan: If by "not necessarily" you meant sometimes it is best to avoid using `using` altogether, I agree. On the other hand, if you meant that sometimes `using namespace` is preferred, I agree with Als. – David Hammen Aug 07 '11 at 14:58
  • @David Rodríguez - dribeas: I just had a glance at the relevant parts in the standard and I will be frank that I don't feel inclined to delve too much deep in to those long paras and get more detail. This is partly because I am lazy right now(being a sunday) & atleast for me it is hard to understand the real meaning of quotes in standard unless I give it sufficiently long time to sink in, which I don't feel inclined to do in this case. – Alok Save Aug 07 '11 at 15:14
4
  • using namespace boost makes all names in the boost namespace visible without qualification
  • using boost::shared_ptr just makes shared_ptr visible without qualification.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
3

The first is called using declaration;

The second is called using directive.

Quoting MSDN:

Note the difference between the using directive and the using declaration:

the using declaration allows an individual name to be used without qualification,

the using directive allows all the names in a namespace to be used without qualification.

Community
  • 1
  • 1
zw324
  • 26,764
  • 16
  • 85
  • 118
1

The first only allows you to use the name shared_ptr without the boost:: prefix. The second allows you to use any and all names in the boost namespace withoout the boost:: prefix. Some people frown on the latter but it's never given me any problems.

john
  • 85,011
  • 4
  • 57
  • 81