草庐IT

c++ - 为什么编译器(例如 gcc)以这种方式处理派生类的内存布局?

这是我的cpp代码。#includeusingnamespacestd;classA{public:intval;chara;};classB:publicA{public:charb;};classC:publicB{public:charc;};intmain(){cout程序的输出(在gcc中)是:81212这个输出让我很困惑。我知道对齐可能是sizeof(A)等于8的原因。(sizeof(int)+sizeof(char)+3字节填充)而且我还猜测sizeof(B)的扩展(sizeof(B)==sizeof(A)+sizeof(char)+3bytespadding)是为了避免

c++ - 结构化绑定(bind)和基于范围的;在 gcc 中抑制未使用的警告

我想使用结构绑定(bind)遍历一个映射,忽略键:for(auto&[unused,val]:my_map)do_something(val);我用gcc-7.2.0尝试了不同的选项://Thewarningisissuedfor([[maybe_unused]]auto&[unused,val]:my_map)do_something(val);//Syntaxerrorfor(auto&[[[maybe_unused]]unused,val]:my_map)do_something(val);//Thesametwocombinationsabovewith[[gnu::unuse

c++ - gcc 和 clang 抛出 "no matching function call"但 msvc (cl) 编译并按预期工作

我写了一个小的函数模板,将不同的容器连接到一个新的容器中:#include#include#include#include#includenamespaceimpl{templatevoidjoin(OutIteratoriterator,constContainer&container,constContainers&...containers){for(constauto&item:container)*iterator++=item;join(iterator,containers...);//gccandclangcannotresolvethiscall}templatevo

c++ - 为什么 gcc 提示 "declaration of ' foo' 隐藏了之前的调用 [-Werror=shadow]”

我有这个MCVE:autobar()->double{return8.0;}intmain(){if(autofoo=bar()){returnfoo;}elseif(autofoo=bar()){returnfoo;}}使用gcc7.3和这些选项-c-Werror-Wextra-Wall-Wshadow编译它会生成以下错误消息:test-shadow.cpp:Infunction‘intmain()’:test-shadow.cpp:9:17:error:declarationof‘foo’shadowsapreviouslocal[-Werror=shadow]elseif(aut

c++ - 原始双类型比较的 GCC 问题

我有以下代码,但是当使用带有各种优化标志的GCC4.4编译它时,我在运行时得到了一些意想不到的结果。#includeintmain(){constunsignedintcnt=10;doublelst[cnt]={0.0};constdoublev[4]={131.313,737.373,979.797,731.137};for(unsignedinti=0;i编译时:"g++-pedantic-Wall-Werror-O1-otesttest.cpp"我得到以下输出:"error@:3"编译时:"g++-pedantic-Wall-Werror-O2-otesttest.cpp"我得

c++ - 在带有 GCC 的生产环境中使用 C++11

按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭11年前。C++11为我们提供了许多非常有用的新工具。GCCsupportofC++11已经取得了不错的进展。所以我有想过什么时候切换到C++11。这个问题只与gcc有关,我不希望用任何其他编译器编译我的(我们的)代码。在gcc支持整个C++11标准以受益于已实现的功能之前,您会(是否)切换到C++11?在稳定性和正确性非常重要的生产环境中,您还会这样做吗?您认为

c++ - 为什么 gcc 不能内联可以确定的函数指针?

以下程序在centos的gcc4.6.2下用-O3编译:#include#include#include#includeusingnamespacestd;templateclassF{public:typedefvoid(T::*Func)();F(Funcf):f_(f){}voidoperator()(T&t){(t.*f_)();}private:Funcf_;};structX{X():x_(0){}voidf(){++x_;}intx_;};intmain(){constintN=100000000;vectorxv(N);autobegin=clock();for_eac

c++ - MacPorts GCC 4.7 OS-X Mavericks 10.9 C11 中的 undefined symbol “toupper”

编辑2:下面是一个程序示例:#include#includeintmain(){inti=0;charstr[]="TestString.\n";charc;while(str[i]){c=str[i];putchar(toupper(c));i++;}return0;}1)clang:clang++-std=c++0x-stdlib=libc++-lc++main.cc-omain编译正常。2)g++-mp-4.8-std=c++11main.cc-omain给出:Undefinedsymbolsforarchitecturex86_64:"toupper(int)",referen

c++ - 推迟 C++ 静态对象构造 - Linux 上的 GCC

假设我有一个名为MyClass的C++类。想象一下,我无法访问MyClass的源代码...它包含在一个库中,我只获得了MyClass的库和头文件>.假设类本身需要环境预配置……例如……在调用类的构造函数之前,我需要做一些设置。该类通常按如下方式使用:voidfunc(){doGlobalSetup();MyClassmyInstance(1,2,3);myInstance.doSomething();...}现在我遇到了需要创建类的全局实例的情况,例如:MyClassmyInstance(1,2,3);intmain(intargc,char*argv[]){doGlobalSetup

c++ - strcpy_s 不适用于 gcc

我有一个C++11项目,我添加了一些strcpy_s方法调用。这适用于Windows,但是在gcc上编译时,会出现错误声明未找到strcpy_s符号。我确实添加了行#define__STDC_WANT_LIB_EXT1__1代码,无济于事。 最佳答案 GCC(或者更确切地说,glibc)不支持strcpy_s()和friend。有关在哪里可以找到支持它们的库的一些想法,请参见此处:Arethereanyfreeimplementationsofstrcpy_sand/orTR24731-1?