草庐IT

c++ - std::is_const 将 const 指针标识为非常量

我很困惑std::is_const识别const的行为指针为非const.我自己的实现is_const做完全一样的事情。我不确定为什么更通用的模板化结构正在挑选版本。gcc4.7和clang3.1-svn表现出相同的行为。任何人都可以解释发生了什么事吗?代码如下:#include#include#includeclassCEmptyClass{};namespacejbc{templatestructis_const:std::false_type{};templatestructis_const:std::true_type{};}intmain(intargc,char*argv[

c++ - 为什么即使在非常简单的情况下,volatile vars 也没有得到优化?

如果我编译代码intmain(){inti;i=1;i=2;}在带有发布和优化的VS中,反汇编看起来像:intmain(){inti;i=1;i=2;}010D1000xoreax,eax010D1002ret但是如果我写“volatile”这个词:intmain(){01261000pushecxvolatileinti;i=1;01261001movdwordptr[esp],1i=2;01261008movdwordptr[esp],2}0126100Fxoreax,eax01261011popecx01261012ret有谁知道为什么VS留下这段代码?它有任何副作用吗?它是程序

c++ - 如何在 C++ 中打印非常大的数字

我有这个代码#includeusingnamespacestd;intmain(intargc,char**argv){unsignedlonglongnum1=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

c++ - 公开 begin() 和 end() 的 const 和非常量版本,以使用智能指针迭代成员 vector

//目录.hclassCat{public:voidconst_meow()const{...};voidmeow(){...};};classCatLibrary{public:std::vector>::iteratorbegin(){returnm_cat_list.begin();}//compileerror,thecompilercomplainscannotcoverttype//from`std::vector>::const_iterator`//to`std::vector>::const_iterator`std::vector>::const_iteratorb

c++ - 为什么 clang 让我在 C++03 模式下通过非常量引用获取临时值?

Inspiredbymyobservationinapreviousquestion,我决定做一个小测试:#include#includeintmain(){charc='A';std::stringstreamss("B");//Iknowthisisbadmojo;that'swhyI'mtestingitss>>char(c);std::cout我的编译器版本:AppleLLVMversion5.1(clang-503.0.40)(basedonLLVM3.4svn)Target:x86_64-apple-darwin13.3.0Threadmodel:posix用C++03模式

c++ - 避免使用 auto 关键字字面上重复 const 和非常量的代码?

好的,我做了一些研究,显然在这个主题上有很多重复的问题,仅举几例:Elegantsolutiontoduplicate,constandnon-const,getters?Howtoavoidoperator'sormethod'scodeduplicationforconstandnon-constobjects?HowdoIremovecodeduplicationbetweensimilarconstandnon-constmemberfunctions?等但我还是忍不住再次提出来,因为与c++14auto类型的返回值,我实际上是在复制函数体,唯一的区别是const函数限定符。c

c++ - 从 main 返回时出现段错误(非常简短的代码,没有数组或指针)

我一直想知道为什么以下琐碎的代码在从main()返回时会产生段错误://Produces"Errorwhiledumpingstate(probablycorruptedstack);Segmentationfault"#include#include#includeusingnamespacestd;classTest{vectornumbers;};intmain(){Testa;ifstreaminfile;cout有趣的是,1)如果只声明了两个变量之一,我不会得到错误,2)如果我声明一个vector变量而不是一个带有vector成员的对象,一切都很好,3)如果我再次声明一个of

c++ - 非常小方阵的特征线性求解器

我正在使用Eigen一个求解极小方阵(4X4)线性方程的C++程序。我的测试代码是这样的templatetypenameEigenSolver>Vertor3dsolve(){//SolveAx=bandAisarealsymmetricmatrixandpositivesemidefinite...//Construct4X4squarematrixAand4X1vectorbEigenSolversolver(A);autox=solver.solve(b);...//Computerelativeerrorforvalidating}我测试了一些EigenSolver其中包括:F

c++ - 在 C++ 11 中将非常量左值引用绑定(bind)到右值是否有效?(已修改)

我知道在c++03中,非常量引用不能绑定(bind)到右值。T&t=getT();无效,在c++11中,我们可以这样做:T&&t=getT();但是上面的代码,应该在c++11中工作吗?我用vs11测试了下面的代码:FoogetFoo(){returnFoo();}voidfz(Foo&f){}intgetInt(){returnint();}voidiz(int&i){}intmain(){{Foo&z=getFoo();//okfz(getFoo());//okint&z2=getInt();//error:initialvalueofreferencetonon-constmus