[英]Weird issues with Clang and C++, and uniform initialization
I was attempting to compile and run the example on this page that explores function pointers as a function input.我试图编译并运行此页面上的示例,该示例探索 function 指针作为 function 输入。 The example I was running was the 66 line one about halfway down the page.我正在运行的示例是页面中间的 66 行第一行。
https://www.learncpp.com/cpp-tutorial/function-pointers/ https://www.learncpp.com/cpp-tutorial/function-pointers/
I am on Mac iOS 12.3.1.我在 Mac iOS 12.3.1 上。 I tried to compile with我试着编译
g++ sort.cc
and was getting errors that no semicolons were in my for loops, i believe due to the bracket initialization throughout the code.并且出现错误,我的 for 循环中没有分号,我相信是由于整个代码中的括号初始化。 And when I run it with:当我运行它时:
g++ -std=c++11 sort.cc
It works fine.它工作正常。
BUT但
Shouldn't my clang be compiling at at least C++11?我的 clang 不应该至少编译 C++11 吗? running跑步
clang -v
I get我明白了
Apple clang version 13.1.6 (clang-1316.0.21.2)
Target: x86_64-apple-darwin21.4.0
And from what I can tell clang versions past 4 default to c++14.据我所知,clang 版本超过 4 默认为 c++14。
Also, when I run using clang or gcc I get errors setting -std=c++xx, but it works fine with g++. But as far as I can tell, g++ and gcc are aliases to clang, and running gcc -v or g++ -v gives me clang version 13.1.6.此外,当我使用 clang 或 gcc 运行时,我收到错误设置 -std=c++xx,但它适用于 g++。但据我所知,g++ 和 gcc 是 88150839870888 和 88150839870888 的别名g++ -v 给我 clang 版本 13.1.6。
So whats going on?发生什么了?
Xcode clang defaults to C++98. Xcode clang 默认为 C++98。 Compiling a simple program which has C++11 or later features will tell you that C++11 or later isn't the default.编译一个具有 C++11 或更高版本功能的简单程序会告诉您 C++11 或更高版本不是默认值。 eg,例如,
// a.cpp
constexpr int i = 10;
clang a.cpp -c
a.cpp:1:1: error: unknown type name 'constexpr'
constexpr int i = 10;
^
1 error generated.
while clang a.cpp -c -std=c++11
works fine.而clang a.cpp -c -std=c++11
工作正常。
Also see: https://trac.macports.org/wiki/CompilerSelection另见: https://trac.macports.org/wiki/CompilerSelection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.