What is the difference between these two usage of using keyword:
using boost::shared_ptr;
and
using namespace boost;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Includes only the
shared_ptrfrom theboostnamespace in your current namespace.This means you can use the
shared_ptrwithout qualifying it with namespaceboost.It is called a using declaration.
Includes all the symbols in the
boostnamespace in your current scope.This means you can use all the symbols in the
boostnamespace without qualifying them with namespaceboost.It is called as using directive.
Why should you always prefer
using declarationoverusing 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 Sutteron the usage ofusing directive:I find it helpful to think of a
using directiveas a marauding army of crazed barbarians that sows indiscriminate destruction wherever it passes–something that by its mere presence can causeunintended conflicts,even when you think you’re allied with it.