为什么操作符“”隐藏在命名空间中? [英] Why is operator""s hidden in a namespace?

查看:237
本文介绍了为什么操作符“”隐藏在命名空间中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要为 std :: string 使用 operator c> using namespace std :: string_literals 。但是不以 _ 开头的用户定义文字是保留的,因此可能的冲突不能成为借口。另一个运算符是从 std :: chrono ,但是这是为int文字,所以没有冲突

In order to use operator""s for std::string you have to do using namespace std::string_literals. But user-defined literals that don't begin with _ are reserved, so possible conflict can't be an excuse. The other operator""s is from std::chrono but that's for int literals, so there's no conflict there either.

这是什么原因?

推荐答案

两个原因为什么字面量放入命名空间:

There are actually two reasons why literals are put into namespaces:


  1. 用户不希望使用 using namespace std; 只是为了获取相应的文字。在这些特定的命名空间中声明文字不会导致问题。

  2. 根据域,可能需要使用 s 作为其他东西的后缀。已有另一个后缀 s 表示秒,但它们没有真正冲突。

  1. It is considered undesirable that users would use using namespace std; just to get hold of the corresponding literals. Having the literals declared in namespaces specific to these doesn't cause the problem.
  2. Depending on the domain it may be desirable to use s as the suffix for something else. There is already another suffix s to mean seconds but they don't really conflict.

STL的CppCon 2014演讲视频(由remyable发表评论)Stephan T 。Lavavej解释了C ++ 14中文字的整体设计,并且非常清楚,它们不是应该在全局命名空间中!相反,标准库中的文字后缀存在于 inline 命名空间的层次结构中,使用户能够对提供的字面进行细粒度的控制。例如,字符串的字面后缀声明为这样(21.3 [string.classes]段落1):

In the video of STL's CppCon 2014 talk (posted by remyable in a comment) Stephan T. Lavavej explains the overall design of the literals in C++14 and its pretty clear that they are not supposed to be in the global namespace! Instead, the literal suffixes in the standard library live in a hierarchy of inline namespaces giving users fine-grained control over the literals being made available. For example, the literal suffix for strings is declared like this (21.3 [string.classes] paragraph 1):

namespace std {
    inline namespace literals {
        inline namespace string_literals {
            string operator"" s(char const* str, size_t len);
        }
    }
}

$ c> inline 命名空间可以让用户选择合适的字面后缀:

This hierarchy of inline namespaces makes it possible for users to get the appropriate choice of literal suffixes:


  • 使用命名空间std; - 您可以获取标准C ++库中的所有内容,包括字面后缀,而不需要任何限定。

  • 使用命名空间std :: literals; - 获取在标准C ++库中定义的所有文字后缀。

  • using namespace std ::

  • 使用命名空间std :: literals :: string_literals; - 是的,你可以这样做,但你真的不应该:这等效于 using namespace std :: string_literals;

  • using namespace std; - you get everything in the standard C++ library, including the literal suffixes, without any qualification.
  • using namespace std::literals; - you get all literal suffixes defined in the standard C++ library.
  • using namespace std::string_literals; - you get all literal suffixes applicable to strings.
  • using namespace std::literals::string_literals; - yes, you can do that but you really shouldn't: that's equivalent to using namespace std::string_literals;.

显然,如果委员会考虑了用字面后缀来污染全局命名空间的想法,那么委员会不会付出那么多努力,虽然他们甚至不能与任何用户字面后缀冲突。

Clearly, the committee wouldn't have gone to that much effort if it had considered the idea viable to just pollute the global namespace with literal suffixes, although they can't even conflict with any user literal suffixes.

这篇关于为什么操作符“”隐藏在命名空间中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆