0

i have noticed that

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[]){
    ....
}

uses using boost::asio::ip::tcp and not using namespace where tcp is a class. can some one tell me whats the benefit of writing it in such a way?

Mafahir Fairoze
  • 697
  • 2
  • 9
  • 21

2 Answers2

3

You don't populate the global namespace with all the contents of namespace boost::asio::ip.

You only use what you need. Take the following example:

namespace A
{
   void foo() {}
   void goo() {}
}

namespace B
{
   void foo() {}
   void goo() {}
} 

If you were to do

using namespace A;
using namespace B;

You would get an ambiguity when trying to call the methods.

But you can say something like:

using A::foo;
using B::goo;

and the ambiguity will be gone.

Of course, the safest way to do it is not using using at all and fully qualify the names on each use.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

It is a using declaration (as opposed to using directive.

Typing boost::asio::ip::tcp; will give you a "shortcut" to the tcp class, but not to the rest of the boost::asio::ip namespace.

Julien P
  • 323
  • 3
  • 8