草庐IT

const_buffers

全部标签

c++ - 将参数作为 const 引用与普通引用传递

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Howcomeanon-constreferencecannotbindtoatemporaryobject?有这样的代码:voidfun_ref(int&par){}voidfun_const_ref(constint&par){}intmain(){//fun_ref(2);error:invalidinitializationofnon-constreferenceoftype‘int&’fromatemporaryoftype‘int’fun_const_ref(2);charvar=3;//fun_

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++ - 指针到指针到 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++ - 指向 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++ - 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++ - 带有 Protocol Buffer 的 RPC

我正在尝试使用ProtocolBuffer和zeromq制作rpc。这是我的原型(prototype)文件:messageSearchRequest{requiredstringquery=1;}messageSearchResponse{repeatedResultresult=1;}messageResult{requiredstringurl=1;optionalstringtitle=2;repeatedstringsnippets=3;}serviceSearchService{rpcSearch(SearchRequest)returns(SearchResponse);}

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

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

c++ - 全局分配函数和 const void*

根据C++11,下面的代码是否构成“未定义行为”(由于使用了const_cast,请参见下面的引用)?constvoid*p=operatornew(123);operatordelete(const_cast(p));来自C++11标准(3.7.4.2.3)的相关引述:Thevalueofthefirstargumentsuppliedtoadeallocationfunctionmaybeanullpointervalue;ifso,andifthedeallocationfunctionisonesuppliedinthestandardlibrary,thecallhasnoeff