Why aren't C++14 standard-defined literals in the global namespace by default?

前端 未结 3 1604
你的背包
你的背包 2021-01-01 14:22

C++14 includes standard-defined literals for, amongst other things, std::string and various timespans from the header.

To us

相关标签:
3条回答
  • 2021-01-01 14:35

    There already are two UDLs named s: one for strings and one for seconds. Due to the understandably terse names of suffixes, they chronically suffer from name conflicts, so pouring all of them into one namespace cannot go well for long. Hence it was decided that they be put into inline namespaces, which allow for both unambiguous (using namespace std::literals::chrono_literals) and simple using directives (using namespace std).

    0 讨论(0)
  • 2021-01-01 14:35

    the standard library already defines multiple versions of what s can mean:

    1. It can be used to define a string literal.
    2. It can be used to define a chrono::seconds literal.

    One is based on a string literal, one is based on an integer or a double literal, of course, i.e., they can actually coexist. However, I'd expect that there may be more uses of s in the future. Thus, having to choose which namespaces are imported rather than getting any imposed on you seems like a reasonable approach.

    0 讨论(0)
  • 2021-01-01 14:38

    Look at paper N2765. UDLs are hooked into the regular name lookup process. As string literals have common string types, there's a large chance of a collision if you ignored namespaces.

    0 讨论(0)
提交回复
热议问题