C++ 'using': Should I use it or should I avoid it?

Posted by Mehrdad on Programmers See other posts from Programmers or by Mehrdad
Published on 2012-03-24T07:29:38Z Indexed on 2012/03/24 11:38 UTC
Read the original article Hit count: 340

Filed under:

I realize there are subtly different semantics for some of these, because of ADL.

In general, though: Which one should I prefer (if any), and why?
(Or does it depend on the situation (e.g. inline header vs. implementation?)

Also: should I prefer ::std:: over std::?

using namespace std;

pair<string::const_iterator, string::const_iterator> f(const string &s)
{
    return make_pair(s.begin(), s.end());
}

or

std::pair<std::string::const_iterator, std::string::const_iterator> f(const std::string &s)
{
    return std::make_pair(s.begin(), s.end());
}

or

using std::pair;
using std::string;

pair<string::const_iterator, string::const_iterator> f(const string &s)
{
    return make_pair(s.begin(), s.end());
}

or

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());
}

or

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());
}

or something else?

(This is assuming I don't have C++11 and auto.)

© Programmers or respective owner

Related posts about c++