草庐IT

c++ - g++ 和 clang++ 不同的行为与静态成员的递归初始化

给定以下代码:#includetemplatestructfoo{staticstd::size_tvalue;};templatestd::size_tfoo::value=0u;templatestd::size_tfoo::value=1u+foo::value;intmain(){std::cout::value::value::value::value其中静态成员value模板结构foo是递归初始化的,我从g++得到不同的输出:3210来自clang++:1110看来g++初始化了foo::value递归地使用foo::value的初始化值其中clang++对foo::val

c++ - "ISO C++ forbids forward references to ' 枚举 ' types"指定枚举基础类型时

给定程序:enumE:int{A,B,C};g++-ctest.cpp工作得很好。但是,clang++-ctest.cpp给出以下错误:test.cpp:1:6:error:ISOC++forbidsforwardreferencesto'enum'typesenumE:int^test.cpp:1:8:error:expectedunqualified-idenumE:int^2errorsgenerated.这些错误消息对我来说没有任何意义。我在这里看不到任何前向引用。 最佳答案 为枚举指定基础类型是C++11语言功能。要编译代

c++ - "ISO C++ forbids forward references to ' 枚举 ' types"指定枚举基础类型时

给定程序:enumE:int{A,B,C};g++-ctest.cpp工作得很好。但是,clang++-ctest.cpp给出以下错误:test.cpp:1:6:error:ISOC++forbidsforwardreferencesto'enum'typesenumE:int^test.cpp:1:8:error:expectedunqualified-idenumE:int^2errorsgenerated.这些错误消息对我来说没有任何意义。我在这里看不到任何前向引用。 最佳答案 为枚举指定基础类型是C++11语言功能。要编译代

c++ - 未找到 macOS Clang C++17 文件系统 header

我需要在我的Mac(macOS10.12.03)上使用(实验性)C++17文件系统库但clang编写程序似乎没有包含文件系统header。因为我需要使用C++17,所以我不能使用Boost库之类的替代方案。当我尝试编译一个仅包含文件系统和iostream的示例程序(并写入cout)#include#includeusingnamespacestd;intmain(){cout我收到以下错误消息:>clangtest.cpp-std=c++1ztest.cpp:2:10:fatalerror:'filesystem'filenotfound#include^1errorgenerated

c++ - 未找到 macOS Clang C++17 文件系统 header

我需要在我的Mac(macOS10.12.03)上使用(实验性)C++17文件系统库但clang编写程序似乎没有包含文件系统header。因为我需要使用C++17,所以我不能使用Boost库之类的替代方案。当我尝试编译一个仅包含文件系统和iostream的示例程序(并写入cout)#include#includeusingnamespacestd;intmain(){cout我收到以下错误消息:>clangtest.cpp-std=c++1ztest.cpp:2:10:fatalerror:'filesystem'filenotfound#include^1errorgenerated

c++ - 考虑到危险,为什么项目使用 -I include 开关?

阅读-I的细则在GCC中切换,我很震惊地发现在命令行上使用它会覆盖系统包括。来自preprocessordocs"Youcanuse-Itooverrideasystemheaderfile,substitutingyourownversion,sincethesedirectoriesaresearchedbeforethestandardsystemheaderfiledirectories."他们似乎没有撒谎。在具有GCC7的两个不同Ubuntu系统上,如果我创建文件endian.h:#error"Thisendian.hshouldn'tbeincluded"...然后在同一目

c++ - 考虑到危险,为什么项目使用 -I include 开关?

阅读-I的细则在GCC中切换,我很震惊地发现在命令行上使用它会覆盖系统包括。来自preprocessordocs"Youcanuse-Itooverrideasystemheaderfile,substitutingyourownversion,sincethesedirectoriesaresearchedbeforethestandardsystemheaderfiledirectories."他们似乎没有撒谎。在具有GCC7的两个不同Ubuntu系统上,如果我创建文件endian.h:#error"Thisendian.hshouldn'tbeincluded"...然后在同一目

c++ - 什么是 C++ 内联类?

无意中发现Clang编译器允许:inlineclassAAA{};在C++中。这是什么?PS。我将此报告给Clang邮件列表cfe-dev@cs.uiuc.edu,现在等待回复。我会在收到通知后更新这个问题。 最佳答案 如果您希望声明一个在类声明之后直接返回该类对象的函数,则允许这样做,例如:#includeinlineclassAAA{public:AAA(){//Nothing}AAA(constAAA&_Param){std::cout您还应注意在其他非法情况下出现的编译器错误(或警告),例如声明变量而不是A(),还应注意编译

c++ - 什么是 C++ 内联类?

无意中发现Clang编译器允许:inlineclassAAA{};在C++中。这是什么?PS。我将此报告给Clang邮件列表cfe-dev@cs.uiuc.edu,现在等待回复。我会在收到通知后更新这个问题。 最佳答案 如果您希望声明一个在类声明之后直接返回该类对象的函数,则允许这样做,例如:#includeinlineclassAAA{public:AAA(){//Nothing}AAA(constAAA&_Param){std::cout您还应注意在其他非法情况下出现的编译器错误(或警告),例如声明变量而不是A(),还应注意编译

c++ - 为什么这个未使用的变量没有被优化掉?

我玩过Godbolt的CompilerExplorer。我想看看某些优化有多好。我的最小工作示例是:#includeintfoo(){std::vectorv{1,2,3,4,5};returnv[4];}生成的汇编器(通过clang5.0.0,-O2-std=c++14):foo():#@foo()pushraxmovedi,20calloperatornew(unsignedlong)movrdi,raxcalloperatordelete(void*)moveax,5poprcxret正如大家所见,clang知道答案,但在返回之前做了很多事情。在我看来,由于“operatorne