Saturday, 24 August 2013

What are the good practices for including namespaces in C++ that avoid more typing?

What are the good practices for including namespaces in C++ that avoid
more typing?

pI am starting out with C++ and almost everywhere I see there is these 2
sentences at the top./p precode#include lt;iostreamgt; using namespace
std; /code/pre pAs I understand namespaces are something to keep names
separate and have same identifiers in different namespaces. So isn't
including codestd/code in the beginning undoing all the hard work of
creating namespaces? Isn't it better to have something like
codestd::cout/code when I need a certain function from a particular
namespace?/p pWould including the namespace in the particular function
that uses it a good tradeoff between less typing (codestd::cout/code vs.
codecout/code everywhere) and avoiding the name conflicts(abc in foo and
abc in bar avoided using codefoo::abc/code and codebar::abc/code)?
Something like/p precodeint function_using_abc_namespace(int a) { using
namespace abc; //Rest of the function } /code/pre pI wanted to know
because avoiding name conflicts is necessary but avoiding more typing is
also important. If this isn't good way to get a middle ground then what
are the alternatives?/p

No comments:

Post a Comment