草庐IT

c++ - 使用 GCC 优化 C/C++ 循环中的嵌套 if 语句

我正在使用GCC编译器测试C/C++中的各种优化。我目前有一个包含多个嵌套if语句的循环。条件是在程序开始执行时计算的。它看起来有点像这样:boolconditionA=getA();boolconditionB=getB();boolconditionC=getC();//Etc.startTiming();do{if(conditionA){doATrueStuff();if(conditionB){//Etc.}else{//Etc.}}else{doAFalseStuff();if(conditionB){//Etc.}else{//Etc.}}}while(testCondi

c++ - gcc 与 clang、msvc 和 icc : Is this function call ambiguous?

我能得到的所有编译器都同意这很好:templateautofoo(Check,T...)->void;templateautofoo(int,T...)->void;intmain(){foo(7,"");}但是,根据gcc,以下代码(带有不能从函数参数推导的前导模板参数)是不明确的:templateautobar(Check,T...)->void;templateautobar(int,T...)->void;intmain(){bar(7,"");//ambiguousaccordingtogccbar(7);//justfine}另一方面,clang、msvc和icc对此非常满

c++ - 缩小从 `int`(常量表达式)到 `unsigned int` 的转换 - MSVC vs gcc vs clang

constexprinti=100;structF{F(unsignedint){}};intmain(){F{i};}上面的代码片段:使用-Wall-Wextra-Wpedantic在g++7上编译没有警告。使用-Wall-Wextra-Wpedantic在clang++4上编译没有警告。无法在MSVC2017上编译:conversionfrom'constint'to'unsignedint'requiresanarrowingconversion问:这里MSVC是不是错了?liveexampleongodbolt.orginti=100;structF{F(unsignedint

c++ - 显然模棱两可的调用不会导致 GCC 上的编译错误

令我惊讶的是,GCC没有认为以下程序中对foo()的调用不明确:#includestructB1{boolfoo(bool){returntrue;}};structB2{boolfoo(bool){returnfalse;}};structC:publicB1,publicB2{usingB1::foo;usingB2::foo;};intmain(){Cc;//Compilesandprints`true`onGCC4.7.2andGCC4.8.0(beta);//doesnotcompileonClang3.2andICC13.0.1;std::cout上面的函数调用在GCC4.

c++ - 当你是 friend 时,为什么 GCC 不允许从私有(private)嵌套类继承?

问同样的问题:为什么GCC允许从私有(private)嵌套类继承?对于非模板类,它允许从私有(private)嵌套类继承,如果它是一个friend,但不是模板类。是错误吗?templateclassInheritFromBaseMember:publicBase::MemberPrivate//error{usingPrivateMember=typenameBase::MemberPrivate;//worksfine};classMyBase{friendclassInheritFromBaseMember;//anothertrytodeclareitfriendtemplate

c++ - 在新版本的 gcc 上返回隐式不可复制结构的 std::map 时出现编译错误

我在新版本的gcc(4.9+)上遇到了这个奇怪的编译错误。代码如下:#include#include#include#include#includeusingnamespacestd;structptrwrap{unique_ptrfoo;};templatestructholder{holder()=default;holder(constholder&b):t(b.t){}holder(holder&&b):t(std::move(b.t)){}holder&operator=(constholder&h){t=h.t;return*this;}holder&operator=(h

c++ - gcc中除法的优化

这是一些代码(完整的程序在问题后面):templateTfizzbuzz(Tn){Tcount(0);#ifCONSTconstTdiv(3);#elseTdiv(3);#endiffor(Ti(0);i现在,如果我用int调用这个模板函数,那么根据我是否定义CONST,我得到6倍的性能差异:$gcc--versiongcc(GCC)3.4.4(cygmingspecial,gdc0.12,usingdmd0.125)$make-BwrappedintCPPFLAGS="-O3-Wall-Werror-DWRAP=0-DCONST=0"&&time./wrappedintg++-O3-

c++ - gcc 过度对齐的新支持 (alignas)

我很难找到有关GCC的aligned-new警告和gcc-faligned-new选项的更多信息。在gcc7.2.0上编译(没有--std=c++17)并尝试定义一个对齐的结构,例如:structalignas(64)Foo{intx;}只是做一个普通的旧:Foo*f=newFoo();给我以下警告和建议:alignas.cpp:36:25:warning:‘new’oftype‘Foo’withextendedalignment64[-Waligned-new=]Foo*f=newFoo();^alignas.cpp:36:25:note:uses‘void*operatornew(

c++ - 为什么 GCC 不能向量化这个函数和循环?

我正在尝试使函数启用SIMD,并通过函数调用对循环进行矢量化。#include#pragmaompdeclaresimddoubleBlackBoxFunction(constdoublex){return1.0/sqrt(x);}doubleComputeIntegral(constintn,constdoublea,constdoubleb){constdoubledx=(b-a)/n;doubleI=0.0;#pragmaompsimdreduction(+:I)for(inti=0;i对于上面的代码,如果我用icpc编译:icpcworker.cc-qopenmp-qopt-r

C++ gcc 字符串内联

我想在运行时通过汇编指令强制将字符串动态分配到局部变量中,没有字符串占用数据部分中的内存(例如读取只有数据部分)。以下似乎完美地工作:charfoo[]="bar";汇编代码变为:movl$7496034,40(%esp)因此,foo在运行时通过movl指令用"bar"初始化。我怎样才能强制它发生在所有字符串操作上?例如,如果我将字符串文字传递给函数:testfunc("bar");在这种情况下,字符串"bar"将被分配到一个节中。 最佳答案 您展示的技术仅适用于您的特殊情况。通常,编译器可以随意将字符串的内容放入段中。例如,通过这