草庐IT

c++ - 正在阅读一个不是最近用 GCC 编写的未定义行为的成员吗?

C++reference有以下用于union的explanation,这个问题的有趣部分以粗体显示:Theunionisonlyasbigasnecessarytoholditslargestdatamember.Theotherdatamembersareallocatedinthesamebytesaspartofthatlargestmember.Thedetailsofthatallocationareimplementation-defined,andit'sundefinedbehaviortoreadfromthememberoftheunionthatwasn'tmos

c++ - gcc 预编译 header 的奇怪行为

我在让预编译的头文件工作时遇到了麻烦,所以我想到了以下最小工作示例。这是头文件foo.h#includeusingnamespacestd;voidhello(){cout我将它编译为g++-cfoo.h给我一个编译后的头文件foo.gch。我希望当我编译以下包含foo.h的源文件时,它应该选择headerfoo.h.gch并且我很好。//test.cpp#include//Swaporderinglater#include"foo.h"//------------------intmain(){hello();}但令人惊讶的是,这并没有使用foo.h.gch进行编译,而是使用了foo

c++ - 关于 C++ 编译器的几个问题 : GCC, MSVC、Clang、Comeau 等

我有几个关于C++编译器的问题C++编译器是否需要one-passcompiler?标准是否在任何地方谈论它?特别是GCC是一次性编译器吗?如果是,那么为什么它会在thisexample中生成以下错误两次(尽管每个错误消息中的模板参数都不同)?error:declarationof‘adderitem’shadowsaparametererror:declarationof‘adderitem’shadowsaparameter一个更一般的问题one-passcompiler的优点和缺点是什么?和multi-passcompiler?有用的链接:AListofC/C++compiler

c++ - gcc -lpthread 不工作

我的系统中安装了ubuntu11。我有一个使用pthread库的c程序。我收到错误Undefinedreferencetosem_wait()即使我使用标志-lpthread编译。例如:gcc-lpthreadprog.c该程序在其他ubuntu安装上运行良好。 最佳答案 尝试:gcc-pthread而不是-lpthread。我相信,差异是显着的。后者链接到libpthread,前者链接到libpthread和其他一些东西!sem_wait是librt的一部分,因此您也可以使用gcc-lrt,但是-pthread会为您完成此操作(并

c++ - 如何修复 gcc -Wall "embedded '\0' in format"警告

这可能不是很重要,但我正在尝试修复g++提示的所有警告。在下面的代码中,我收到了snprintf()行的“embedded'\0'informat”警告。我该如何解决这个问题?intfilePathSize=path.size()+s.size()+1;charfilePath[filePathSize];snprintf(filePath,filePathSize,"%s%s\0",path.c_str(),s.c_str());提前致谢... 最佳答案 警告是有充分理由的:snprintf将认为\0标记字符串的结尾。如果您确实需

c++ - gcc编译C++代码: undefined reference to `operator new[](unsigned long long)'

有一段C++代码:#includeintmain(){intb=sizeof('a');if(b==4)printf("I'maCprogram!\n");elseprintf("I'maC++program!\n");}像这样编译:gccmain.cpp-omain它成功并给出:I'maC++program!然后在函数main的某处添加一行int*p1=newint[1000];它失败了:C:\Users\...\AppData\Local\Temp\cccJZ8kN.o:main1.cpp:(.text+0x1f):undefinedreferencetooperatornew[]

c++ - 如何在 gcc 中声明和定义一个纯函数?

GCC具有pure和const属性,其中const实际上用于真正的纯函数(pure用于idempotentfunctionswhicharealsoside-effectfree)。那么如何使用常量属性声明和定义函数呢?编辑:我对真正的纯函数感兴趣,那些用const属性声明的函数,而不是那些用pure属性声明的函数。 最佳答案 例子://Declaration:intsquare(intx)__attribute__((const));//Definition:int__attribute__((const))square(intx

c++ - 为什么 gcc 会在全局命名空间中隐藏重载函数?

voidf(){}namespacetest{voidf(int){}voidg(){f();}//erroringcc6.2.0}intmain(){test::g();}用g++-std=c++1zmain.cpp编译,输出如下:main.cpp:Infunction'voidtest::g()':main.cpp:9:4:error:toofewargumentstofunction'voidtest::f(int)'f();//erroringcc^main.cpp:5:6:note:declaredherevoidf(int){}我的编译器是gcc6.2.0。为什么gcc会在

c++ - gcc 在这个概念定义中错误地评估了 std::declval 吗?

在这个概念定义中:#includetemplateconceptInvokable=requires(Funcf){{f(std::declval()...)}->Ret;};像这样实例化时:static_assert(Invokable);gcc-9.0.1(主干)转储(好吧,准确地说是标准库实现):$g++-O2-std=c++2a-fconcepts-Wall-Wextra-Werror-ctu1.cpperror:staticassertionfailed:declval()mustnotbeused!2204|static_assert(__declval_protector

c++ - avr-gcc:(看似)简单功能中不需要的序言/结尾

当尝试处理uint64中的单个字节时,AVRgcc⁽¹⁾给我一个奇怪的序言/结尾,而使用uint32_t编写的相同函数给了我一个单个ret(示例函数是NOP)。为什么gcc这样做?我该如何删除它?Youcanseethecodehere,inCompilerExplorer.⁽¹⁾来自Arduino1.8.9发行版的gcc5.4.0,参数=-O3-std=c++11。源代码:#includeuint32_tf_u32(uint32_tx){uniony{uint8_tp[4];uint32_tw;};returny{.p={y{.w=x}.p[0],y{.w=x}.p[1],y{.w=