草庐IT

c++ - 函数指针的模板参数推导(g++ & ICC vs Clang++ & VC++)

考虑以下程序:#includetemplatevoidfoo(constT*x){x();}voidbar(){std::cout它在clang++和VC++上编译良好,但g++给出以下编译器错误(参见现场演示here)main.cpp:Infunction'intmain()':main.cpp:10:9:error:nomatchingfunctionforcallto'foo(void(&)())'foo(bar);^main.cpp:3:6:note:candidate:templatevoidfoo(constT*)voidfoo(constT*x){^~~main.cpp:

c++ - g++ 编译错误

我是Ubuntu的新手。我试图编译一个简单的“HelloWorld!”Ubuntu11.04中的c++代码,带有该代码(在终端中):gcc-Wall-W-Werrortex.cpp-otex.但是编译器返回了很多错误:/tmp/ccL8c1p8.o:Infunction`main':tex.cpp:(.text+0xa):undefinedreferenceto`std::cout'tex.cpp:(.text+0xf):undefinedreferenceto`std::basic_ostream>&std::operator>(std::basic_ostream>&,charco

c++ - Cygwin 与 Linux 下 g++ 创建的可执行文件的默认文件扩展名

我的大部分工作都是在VisualStudio上完成的,对gcc或g++没有太多经验。今天早上,当我尝试在我的电脑上使用cygwin编译一个(例如aprogram.cpp)时,我得到了(aprogram.exe),当我试图在我的Ubuntu机器上编译相同的东西时,我得到了(aprogram)w/o任何扩展.我只是想知道是否有人好心告诉我原因。这个问题只是出于好奇。:)提前致谢!编辑:(来自吉米的评论)Cygwin下的g++默认为.exe 最佳答案 这很简单:在UNIX上,youdon'tneednosteenkin'extension

c++ - 候选模板被忽略 : substitution failure(error with clang but not g++)

我有一个替换失败的问题,一些类似问题的答案对我没有帮助。代码如下:templateclassReference{public://...templateusingmatrix_t=int[r][c];Reference(constmatrix_t&mat){}};templateclassPartition{//...public://...templateusingmatrix=int[r][c];templatevoidreadPattern(constmatrix&pattern){//...}//...};我这样调用这个模板函数:intmain(){//...constintD

c++ - g++ 和 clang++ 与在模板类中定义的友元模板函数的不同行为

另一个类型的问题“g++和clang++之间谁是正确的?”适用于C++标准专家。下面的代码templatestructfoo{templatefriendvoidbar(){}};intmain(){foof0;foof1;}使用clang++编译没有问题(只有两个“未使用的变量”警告)但给出以下错误tmp_002-11,14,gcc,clang.cpp:Ininstantiationof‘structfoo’:tmp_002-11,14,gcc,clang.cpp:27:12:requiredfromheretmp_002-11,14,gcc,clang.cpp:20:16:erro

c++ - g++ 6.2 的不同异常说明符

有人能给我解释一下为什么这段代码不能用g++版本6.2.0编译,但是可以用clang++版本3.9.0-svn274438-1和icpc版本16.0.2编译吗$catwtf.cpp#include#includevoid*operatornew(std::size_t)throw(std::bad_alloc);void*operatornew(std::size_t)throw(std::bad_alloc){void*p;returnp;}$g++-6wtf.cpp-cwtf.cpp:Infunction‘void*operatornew(std::size_t)’:wtf.cpp

c++ - VS2015 和 clang 编译此代码,但 g++ 拒绝它。哪一个是正确的?

VS2015和clang编译这段代码,但是g++rejectsit.namespaceA{structB{friendvoidf();};}voidA::f(){}intmain(){}我认为g++是正确的,因为7.3.1.2/3中的注释:Ifafrienddeclarationinanon-localclassfirstdeclaresaclass,function,classtemplateorfunctiontemplate97thefriendisamemberoftheinnermostenclosingnamespace.Thefrienddeclarationdoesno

c++ - 为什么g++ 5.4不能编译这段编译期素数代码?

#includeusingnamespacestd;templateclassPrime{//generateNprimenumbersatcompiletimepublic:unsignedintarr[N]{};constexprPrime(){intk=0;for(unsignedinti=2;ki/2)break;if(i%arr[j]==0){isPrime=false;break;}}if(isPrime)arr[k++]=i;}}};intmain(){Primeprime;//if50000->5000,okfor(auto&a:prime.arr)coutG++无法编

c++ - 用流操作替换 printf ("%g", value)

我想替换以下实现:floatvalue=3.14;printf("%g",value);(如果需要,请参阅How%gworksinprintf了解%g的解释)。但我还没有在流操纵器中找到等效项,仅适用于固定或科学,但不是两者中最短的(https://en.cppreference.com/w/cpp/io/manip/fixed)。这是否存在或是否有实现它的“简单”方法?链接的SO问题中的一些示例:如果使用%.6g,544666.678写成544667,当使用%.5g时,相同的数字写成5.4467E+5。 最佳答案 %g是默认行为。

c++ - 为什么 g++ 和 MS Visual Studio C++ 执行以下代码的方式不同?

我无法理解是哪个编译器出了问题(如果有的话)。与MSVisualStudioC++相比,以下代码在g++中的执行方式不同。#includeintmain(){inta=10;//somerandomvalueint*ptr=&a;//atemprvalueoftype`constint*const'createding++//notempcreatedinMSVisualStudioconstint*const&alias_for_ptr=ptr;ptr=0;//nullptrif(ptr==alias_for_ptr)//ThiswillexecuteinMSVisualStudio