【发布时间】:2014-01-10 17:50:00
【问题描述】:
我安装了 Clang 3.4,并测试字符串文字 ""s 的后缀运算符。
#include <string>
//1. using namespace std; // without compile error.
//2. using std::operator ""s; // or without it compile error, too.
// for success compiles, need 1. or 2. line.
int main()
{
auto s = "hello world"s;
}
如果我评论 1 和 2 ,我会得到编译错误。但我知道在大项目中 1.-方法不好, 和 2.-method 比较陌生。
QA:我可以在不写using namespace std 和using std::operator ""s 的情况下使用运算符“”吗?
【问题讨论】:
-
"2.-方法是陌生的。"为什么你觉得这很奇怪?对我来说看起来很自然。
-
简答:不,你不能。这就是它的意图,Clang 要求 1. 或 2 是正确的。看起来你对此并不满意,并想质疑它是如何定义的决定,但这不是一个适合 SO 的 Q。
-
这到底是什么...我们真的需要
using才能使文字后缀起作用?头脑难以置信。 -
这些操作符已经定义在一个内联命名空间中,所以你也可以使用
using namespace std::literals::string_literals;(例如在你使用它们的函数内部) -
using namespace std::literals也可以。