Prefer using std::string that using namespace std and unqualified string (#386)

'using namespace std;' in header files can conflict with custom
namespace names. For example 'string' is used without 'std::'
qualification. If tz.h is included in a file where string is a
namespace, it cannot be compiled anymore. The same happens with date.h
if ONLY_C_LOCALE=1.
This commit is contained in:
F 2018-09-26 23:31:56 +02:00 committed by Howard Hinnant
parent 6b51ca8271
commit 69e9cd612f
2 changed files with 11 additions and 16 deletions

View File

@ -4493,8 +4493,7 @@ inline
std::pair<const std::string*, const std::string*> std::pair<const std::string*, const std::string*>
weekday_names() weekday_names()
{ {
using namespace std; static const std::string nm[] =
static const string nm[] =
{ {
"Sunday", "Sunday",
"Monday", "Monday",
@ -4511,15 +4510,14 @@ weekday_names()
"Fri", "Fri",
"Sat" "Sat"
}; };
return make_pair(nm, nm+sizeof(nm)/sizeof(nm[0])); return std::make_pair(nm, nm+sizeof(nm)/sizeof(nm[0]));
} }
inline inline
std::pair<const std::string*, const std::string*> std::pair<const std::string*, const std::string*>
month_names() month_names()
{ {
using namespace std; static const std::string nm[] =
static const string nm[] =
{ {
"January", "January",
"February", "February",
@ -4546,20 +4544,19 @@ month_names()
"Nov", "Nov",
"Dec" "Dec"
}; };
return make_pair(nm, nm+sizeof(nm)/sizeof(nm[0])); return std::make_pair(nm, nm+sizeof(nm)/sizeof(nm[0]));
} }
inline inline
std::pair<const std::string*, const std::string*> std::pair<const std::string*, const std::string*>
ampm_names() ampm_names()
{ {
using namespace std; static const std::string nm[] =
static const string nm[] =
{ {
"AM", "AM",
"PM" "PM"
}; };
return make_pair(nm, nm+sizeof(nm)/sizeof(nm[0])); return std::make_pair(nm, nm+sizeof(nm)/sizeof(nm[0]));
} }
template <class CharT, class Traits, class FwdIter> template <class CharT, class Traits, class FwdIter>

View File

@ -1963,10 +1963,9 @@ std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const utc_time<Duration>& t) const utc_time<Duration>& t)
{ {
using namespace std;
using namespace std::chrono; using namespace std::chrono;
using CT = typename common_type<Duration, seconds>::type; using CT = typename std::common_type<Duration, seconds>::type;
const string abbrev("UTC"); const std::string abbrev("UTC");
CONSTDATA seconds offset{0}; CONSTDATA seconds offset{0};
auto ls = is_leap_second(t); auto ls = is_leap_second(t);
auto tp = sys_time<CT>{t.time_since_epoch() - ls.second}; auto tp = sys_time<CT>{t.time_since_epoch() - ls.second};
@ -1992,16 +1991,15 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
utc_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr, utc_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr) std::chrono::minutes* offset = nullptr)
{ {
using namespace std;
using namespace std::chrono; using namespace std::chrono;
using CT = typename common_type<Duration, seconds>::type; using CT = typename std::common_type<Duration, seconds>::type;
minutes offset_local{}; minutes offset_local{};
auto offptr = offset ? offset : &offset_local; auto offptr = offset ? offset : &offset_local;
fields<CT> fds{}; fields<CT> fds{};
fds.has_tod = true; fds.has_tod = true;
from_stream(is, fmt, fds, abbrev, offptr); from_stream(is, fmt, fds, abbrev, offptr);
if (!fds.ymd.ok()) if (!fds.ymd.ok())
is.setstate(ios::failbit); is.setstate(std::ios::failbit);
if (!is.fail()) if (!is.fail())
{ {
bool is_60_sec = fds.tod.seconds() == seconds{60}; bool is_60_sec = fds.tod.seconds() == seconds{60};
@ -2012,7 +2010,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
tmp += seconds{1}; tmp += seconds{1};
if (is_60_sec != is_leap_second(tmp).first || !fds.tod.in_conventional_range()) if (is_60_sec != is_leap_second(tmp).first || !fds.tod.in_conventional_range())
{ {
is.setstate(ios::failbit); is.setstate(std::ios::failbit);
return is; return is;
} }
tp = time_point_cast<Duration>(tmp); tp = time_point_cast<Duration>(tmp);