草庐IT

const_cast-ed

全部标签

c++ - 用包含 const 成员的结构实例替换堆栈与静态变量

使用gcc4.4.3版本如下:gcc-g-xc++-lstdc++-std=c++98-o./main./main.cppmain.cpp中的这段代码可以正常编译:#includestructA{A():m_flag(false){}constboolm_flag;};staticAaa=A();intmain(intargc,char*argv[]){Aa;//Notstatic=copyOKAb(a);Ac=b;Ad=A();//Static=copynotOK//aa=A();}但是如果我取消注释aa=A();我会得到:./main.cpp:Inmemberfunction'A&

c++ - static_cast 和临时创建(最终版)

先决条件:要理解这个问题,请先阅读以下问题及其答案:Castauto_ptrtoauto_ptr在Castauto_ptrtoauto_ptr史蒂夫回答说,“您的static_cast会将auto_ptr复制到一个临时文件,因此aS将被重置,当临时文件(在语句末尾)时,资源将被销毁。”我对static_cast时临时创建的过程很感兴趣叫做。我想要我可以跟踪的代码以查看此效果。我不能使用static_cast>...因为它不能被编译,所以我需要写一些模拟类而不是auto_ptr并观看临时创建的过程。我也明白临时创建与复制构造函数调用密切相关。auto_ptr的所有权丢失是通过设置_rad

c++ - 指针到指针到 Const 的转换

我正在读一本名为C++Gotchas的书这解释了const指针之间的转换,我在理解以下规则时遇到了一些麻烦:TwopointertypesT1andT2aresimilarifthereexistsatypeTandintegern>0suchthat:T1iscv1,0pointertocv1,1pointerto...cv1,n−1pointertocv1,nTand,T2iscv2,0pointertocv2,1pointerto...cv2,n−1pointertocv2,nTwhereeachcvi,jisconst,volatile,constvolatile,ornoth

c++ - 在启用 ARC 的情况下使用 reinterpret_cast

我在支持ARC的Objective-C项目中包含了一个库的头文件。我知道库没有在启用ARC的情况下编译,但问题出在库的头文件上,特别是这些行:templatestaticinlineType_&MSHookIvar(idself,constchar*name){Ivarivar(class_getInstanceVariable(object_getClass(self),name));void*pointer(ivar==NULL?NULL:reinterpret_cast(self)+ivar_getOffset(ivar));return*reinterpret_cast(poi

c++ - 指向 const 成员函数的非类型模板函数指针

我正在编写一个委托(delegate)类,但它无法采用const成员函数。这是一个测试用例:classfoo{public:voidMemberFunction(){printf("nonconstmemberfunction\n");}voidConstMemberFunction()const{printf("constmemberfunction\n");}};templatevoidCall(C*instance){(instance->*Function)();}intmain(intargc,char**argv){foobar;Call(&bar);Call(&bar);

c# - 是否有相当于 C# TryParse 的 boost lexical_cast?

简介(来自EricLippert博客):Vexingexceptionsaretheresultofunfortunatedesigndecisions.Vexingexceptionsarethrowninacompletelynon-exceptionalcircumstance,andthereforemustbecaughtandhandledallthetime.TheclassicexampleofavexingexceptionisInt32.Parse,whichthrowsifyougiveitastringthatcannotbeparsedasaninteger.

c++ - forward_list::splice_after( const_iterator pos, forward_list& other, const_iterator i ) 功能

我正在阅读有关此功能工作方式的不同解释。cplusplus.com说这个函数应该“直接在i之后移动元素”。然而cppreference.com表示它拼接元素ATi。MSvisualstudio同意cplusplus.com。但是,实际上正确的行为是什么?我倾向于认为“在i之后”移动更合乎逻辑(&不需要N时间来找到前面的节点)。(PS:没有forward-list标签?) 最佳答案 23.3.4.6voidsplice_after(const_iteratorposition,forward_list&x,const_iterator

c++ - const 限定符在复制初始化变量时有用吗?

我想知道在使用值的拷贝初始化非引用/指针变量时使用可选的const限定符有哪些优点和缺点:例如:voidf(constTv)而不是voidf(Tv)//v不需要改变if(constinterr=f()){/*...*而不是if(interr=f()){/*...*甚至voidf(){constT*constv=p;/*...*而不是voidf(){constT*v=p;/*...*/>这只是风格问题吗?C++11标准在其示例中使用什么?const不能提示编译器将变量存储在一些特殊的只读存储器中(在某些实现中)吗? 最佳答案 在这种情

c++ - const T& 和 T 类型参数的函数匹配

我有一个关于c++函数匹配T和constT&类型参数的问题。假设我有以下两个功能:voidf(inti){}voidf(constint&ri){}如果我用constint类型的参数调用f那么这个调用当然是不明确的。但是,为什么使用int类型的参数调用f也是不明确的?不会是f的第一个版本是完全匹配而第二个版本是更差的匹配,因为int参数必须转换为constint?constintci=0;inti=0;f(ci);//ofcourseambiguousf(i);//whyalsoambiguous?我知道这种重载没有多大意义,因为f的调用几乎总是不明确的,除非参数类型T没有可访问的复制

c++ - 为什么不允许在 const 非 volatile 成员函数上消除公共(public)子表达式?

C++的目标之一是允许用户定义类型的行为与内置类型一样好。这似乎失败的一个地方是编译器优化。如果我们假设const非volatile成员函数在道德上等同于读取(对于用户定义的类型),那么为什么不允许编译器消除对此类函数的重复调用呢?例如classC{...public:intget()const;}intmain(){Cc;intx{c.get()};x=c.get();//whynotallowthecompilertoeliminatethiscall}允许这样做的论点与复制省略的论点相同:虽然它改变了操作语义,但它应该适用于遵循良好语义实践的代码,并在效率/模块化方面提供实质性改