c++ - Why is operator""s hidden in a namespace? -
in order use operator""s
std::string
have using namespace std::string_literals
. user-defined literals don't begin _
reserved, possible conflict can't excuse. other operator""s
std::chrono
that's int literals, there's no conflict there either.
what's reason this?
there 2 reasons why literals put namespaces:
- it considered undesirable users use
using namespace std;
hold of corresponding literals. having literals declared in namespaces specific these doesn't cause problem. - depending on domain may desirable use
s
suffix else. there suffixs
mean seconds don't conflict.
in video of stl's cppcon 2014 talk (posted remyable in comment) stephan t. lavavej explains overall design of literals in c++14 , pretty clear not supposed in global namespace! instead, literal suffixes in standard library live in hierarchy of inline
namespaces giving users fine-grained control on literals being made available. example, literal suffix strings declared (21.3 [string.classes] paragraph 1):
namespace std { inline namespace literals { inline namespace string_literals { string operator"" s(char const* str, size_t len); } } }
this hierarchy of inline
namespaces makes possible users appropriate choice of literal suffixes:
using namespace std;
- in standard c++ library, including literal suffixes, without qualification.using namespace std::literals;
- literal suffixes defined in standard c++ library.using namespace std::string_literals;
- literal suffixes applicable strings.using namespace std::literals::string_literals;
- yes, can shouldn't: that's equivalentusing namespace std::string_literals;
.
clearly, committee wouldn't have gone effort if had considered idea viable pollute global namespace literal suffixes, although can't conflict user literal suffixes.
Comments
Post a Comment