Discounting subtly different semantics due to ADL, how should I generally use using
, and why? Is it situation-dependent (e.g. header which will be #include
d vs. source file which won't)?
Also, should I prefer ::std::
or std::
?
Namespace-level
using namespace
:using namespace std; pair<string::const_iterator, string::const_iterator> f(const string &s) { return make_pair(s.begin(), s.end()); }
Being fully explicit:
std::pair<std::string::const_iterator, std::string::const_iterator> f(const std::string &s) { return std::make_pair(s.begin(), s.end()); }
Namespace-level using-declarations:
using std::pair; using std::string; pair<string::const_iterator, string::const_iterator> f(const string &s) { return make_pair(s.begin(), s.end()); }
Function-local using-declarations:
std::pair<std::string::const_iterator, std::string::const_iterator> f(const std::string &s) { using std::make_pair; return make_pair(s.begin(), s.end()); }
Function-local
using namespace
:std::pair<std::string::const_iterator, std::string::const_iterator> f(const std::string &s) { using namespace std; return make_pair(s.begin(), s.end()); }
Something else?
This is assuming pre-C++14, and thus no return-type-deduction using auto
.
::std::
vs.std::
though. – user541686 Mar 24 '12 at 7:49std
without second though. Someone defining a std namespace is asking for trouble (and probably searching to take advantage that most people are usingstd
and not::std
). – AProgrammer Mar 24 '12 at 11:35