草庐IT

c++ - 将左值绑定(bind)到右值引用——g++ 错误?

作为对anotherquestion的回答我想贴出下面的代码(也就是我想贴出基于这个思路的代码):#include#include//std::is_same,std::enable_ifusingnamespacestd;templatestructBoxed{Typevalue;templateBoxed(Argconst&v,typenameenable_if::value,Arg>::type*=0):value(v){wcoutv){}intmain(){inti=5;function(i);//但是,虽然MSVC11.0在最后一次调用时阻塞,但IHMO应该如此,而MinGW

c++ - 修复了 g++ 4.9.1 奇怪的 "%a"格式行为?

编译器:来自Nuwen发行版的64位MinGWG++4.9.1,在Windows8.1下。代码:#ifdefINCLUDE_IOSTREAM#include#endif#include//::snprintf#include//EXIT_SUCCESS,EXIT_FAILURE#include//std::exception#ifdefsnprintf#errorsnprintfdefinedasmacro#endif#ifdef_MSC_VERautoconstsnprintf=_snprintf;#endifvoidtest(doubleconstvalue,intconstpre

c++ - g++ 和 clang++ 自动参数模板特化的不同行为

使用C++17auto模板参数我遇到了另一个g++/clang++分歧。给定以下简单代码templatestructfoo;templatestructfoo{};intmain(){foof42;//我看到clang++(8.0.0,例如)编译g++(9.2.0,例如)给出以下错误的代码prog.cc:Infunction'intmain()':prog.cc:12:13:error:aggregate'foof42'hasincompletetypeandcannotbedefined12|foof42;|^~~如果我们使用int常量而不是long常量,两个编译器都会编译foof4

c++ - 如何使用 -g 调试标志编译 boost?

我想重新编译我们的boost库,但启用了-g调试标志。我一直在阅读并看到我们使用jam构建了boost。虽然我非常了解make,但jam一直让我望而却步,我的挫败感越来越大。 最佳答案 对不起。我知道回答你自己的问题是令人反感的,但我在发帖后不久就弄明白了。cxxflags和cflags属性是我需要使用的,如下所述:http://www.boost.org/boost-build2/doc/html/bbv2/overview/invocation.html在属性部分下。 关于c++-如

c++ - 如何强制 g++ 内联函数?

我最近在将HaskellFFI用于C/C++时遇到了C++内联函数的问题。即,g++并不真正内联声明为inline的函数,并为它们生成符号。最终,当ghci尝试加载调用内联函数的目标文件时,这会产生链接器错误:Loadingobject(static)solveeq.o...doneLoadingobject(dynamic)/usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.so...donefinallink...ghc:solveeq.o:unknownsymbol`_ZN5Eigen8internal19throw_std_bad_alloc

c++ - cast 运算符函数在 g++ 中编译良好,但在其他编译器中则不然。为什么?

这个问题在这里已经有了答案:Operatorcast,GCCandclang:whichcompilerisright?(1个回答)关闭6年前。考虑以下程序:structS{usingT=float;operatorT(){return9.9f;}};intmain(){Sm;S::Tt=m;t=m.operatorT();//Isthiscorrect?}程序在g++中编译良好(参见现场演示here)但它在clang++、MSVC++和IntelC++编译器中编译失败clang++给出以下错误(参见现场演示here)main.cpp:8:20:error:unknowntypenam

c++ - g++ 可变参数模板问题

所以我把这个程序交给了g++和clang(都在Linux,x86_64上):#includeusingnamespacestd;templatestructA{staticconststrings;staticAa;~A(){coutconststringA::s={{Cs...}};templateAA::a;intmain(void){(void)A::a;return0;}Clang输出s=aaaaaaaaaaaaaaaa(正如预期的那样)。g++(版本5到8)输出s=s=aaaaaaaa(非常出乎意料)。如果您不使用可变参数模板(如果您删除所有代码并内联字符列表以初始化A::s

c++ - G++ 编译器错误或错误代码? : "template definition of non-template"

作为大型程序的特征类的一部分,我尝试创建一个静态类变量,该变量可能具有不同的值,具体取决于实例化封闭类模板的类型。我已经简化了相关代码以生成我正在谈论的内容的简单示例:#include#include#includetemplatestructFoo;templatestructFoo::value>::type>{staticstd::stringmessage;};templatestructFoo::value>::type>{staticstd::stringmessage;};templatestd::stringFoo::message;对于GCC4.6,这会产生一个编译器

c++ - G++、clang++ 和 std::function

我只是在玩弄来自C++11的新std::function,我写了一个使用clang++3.2和英特尔C++编译器13.1编译但不使用g++4.8编译的示例。在我将其报告为错误之前,我想我应该检查一下我没有做一些非常愚蠢的事情,并且这实际上应该可以编译。那么,以下代码是否适用于c++11?templatevoidmap(C&c,std::functionf){for(auto&x:c){x=f(x);}}intmain(){std::vectorv;v.push_back(1);v.push_back(2);v.push_back(3);map(v,[](intx){returnx+2;

c++ - 为什么这段代码在 Clang++ 中有效,但在 G++ 中无效?

考虑以下代码:structFoo{intx,y;Foo()=default;Foo(constFoo&)=delete;Foo&operator=(constFoo&)=delete;};intmain(){Foof1{1,2};Foof2={1,2};}使用clang++编译不会产生错误:$clang++--versionAppleLLVMversion4.2(clang-425.0.28)(basedonLLVM3.2svn)Target:x86_64-apple-darwin12.4.0Threadmodel:posix$clang++-std=c++11-stdlib=libc