Use `using` in C++ or avoid it?
https://softwareengineering.stackexchange.com/questions/141317
Pergunta
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
.
Nenhuma solução correta
Licenciado em: CC-BY-SA com atribuição
Não afiliado a softwareengineering.stackexchange