我使用以下代码来测试我的编译器是否符合c ++ 14:
#include <iostream>
#include <string>
using namespace std;
auto add([](auto a, auto b){ return a+b ;});
auto main() -> int {cout << add("We have C","++14!"s);}
没有错误。然后我尝试通过评论using namespace std;
并将cout
替换为std::cout
来“优化”代码。现在代码看起来像这样:
#include <iostream>
#include <string>
//using namespace std;
auto add([](auto a, auto b){ return a+b ;});
auto main() -> int {std::cout << add("We have C","++14!"s);}
构建消息:
||=== Build: Release in c++14-64 (compiler: GNU GCC Compiler) ===|
C:\CBProjects\c++14-64\c++14-64-test.cpp||In function 'int main()':|
C:\CBProjects\c++14-64\c++14-64-test.cpp|5|error: unable to find string literal operator 'operator""s' with 'const char [6]', 'long long unsigned int' arguments|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
问题:
using namespace std
?答案 0 :(得分:3)
clang++
给出了一条很好的错误消息:
error: no matching literal operator for call to 'operator""s' with arguments of types 'const char *' and 'unsigned long', and no matching literal operator template
auto main() -> int { std::cout << add("We have C", "++14!"s); }
^
您使用字符串文字,更精确地使用operator""s
。
通过删除using namespace std;
,您必须指定定义运算符的命名空间。
通过明确的电话:
int main() {
std::cout << add("We have C", std::operator""s("++14!", 5));
// Note the length of the raw character array literal is required
}
或使用using
声明:
int main() {
using std::operator""s;
std::cout << add("We have C", "++14!"s);
}