问题描述
C ++ 14包括标准定义的字面量,其中包括 std :: string 和来自< chrono>
C++14 includes standard-defined literals for, amongst other things, std::string and various timespans from the <chrono> header.
要使用它们,必须使用命名空间std :: literals (或某些变化取决于你想要的文字,因为他们在各种内联命名空间)。
To use them you must say using namespace std::literals; (or some variation depending on exactly which literals you want, as they're in a variety of inline namespaces).
这一切都很好,但我很好奇为什么需要使用声明。没有前导下划线的UDL保留用于实现,因此hello worlds 不可能表示符合标准的程序中的任何其他内容。
All this is good, but I'm curious as to why the using declaration is required. UDLs without a leading underscore are reserved for the implementation, so there is no possibility that "hello world"s could ever mean anything else in a standard-conforming programme.
那么为什么 #include< string> 足以使文字转换函数为什么必须显式包含文字命名空间?
So why isn't #include <string> sufficient to bring the literal conversion function into scope? Why must I explicitly include the literal namespace?
EDIT: 是我可以找到的提案的最新版本 - 不幸的是,它没有讨论将东西放在命名空间中的动机,只有:
N3531 is the most recent version of the proposal I could find -- unfortunately it doesn't discuss the motivation for putting things in a namespace but only says:
- 为(一组相关的)UDL运算符使用内联命名空间
推荐答案
因为它容易产生冲突,因为名称是单个字符。事实上,已经有两个名为 s 的UDL:一个用于字符串,一个用于。问题是,在某些阶段,我们被迫使用不适合,较长的后缀,或在事后引入命名空间,这是两个杂乱的选择。
Because it easily generates conflicts, since the name is a single character. In fact, there already are two UDLs named s: One for strings and one for seconds. The problem is that at some stage, we are forced to either use unfitting, longer suffixes, or introduce namespaces in hindsight, which are both messy options.
(使用命名空间std :: literals :: chrono_literals )将UDL放入内联命名空间, )和简单指令(使用命名空间std )。
So it was decided that UDLs are put into inline namespaces, which allow for unambiguous using directives (using namespace std::literals::chrono_literals) and simple directives (using namespace std).
这篇关于为什么默认情况下在全局命名空间中没有C ++ 14标准定义的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!