草庐IT

const_cast

全部标签

c++ - 应该如何使用 const/non-const 参数重载函数?

我有以下代码://stringspecializationsvoidfoo(constchar*a,constchar*b);voidfoo(constchar*a,conststd::string&b);voidfoo(conststd::string&a,constchar*b);voidfoo(conststd::string&a,conststd::string&b);//genericimplementationtemplatevoidfoo(TAa,TAb){...}问题是这个测试用例:chartest[]="test";foo("test",test);最终调用了foo的

c++ - static_cast 如何与虚拟继承一起使用?

因此不可能使用具有虚拟继承的static_cast进行向下转型,但是如何进行以下向上转型:classBase{...};classDerived:publicvirtualBase{...};...Derived*d=newDerived();Base*b=static_cast(d);对象的内存布局:[derivedpart|basepart]我知道向上转型被认为是“安全的”,但是当继承是虚拟的时,编译器如何在编译时知道基础子对象的偏移量?static_cast是否使用vtable?当我们有这样的东西(注意它不是虚拟的)时,这尤其令人困惑:classThird:publicDeriv

c++ - std::set<K, C>::operator<(const std::set<K, C>&) 不使用 C() 但 std::less()

无法删除我自己的问题,所以改写它... 最佳答案 这实际上不是实现中的错误,尽管它可以说是标准中的错误:23.2.1Generalcontainerrequirements[container.requirements.general]13Table98listsoperationsthatareprovidedforsometypesofcontainersbutnotothers.Thosecontainersforwhichthelistedoperationsareprovidedshallimplementtheseman

c++ - 使用 const 引用的函数模板重载决议

我试图理解以下情况下的重载决议规则:templatevoidf(constT&x){std::coutvoidf(T&x){//ÜberladungVariante2std::cout输出是:voidf(T&)[T=int]voidf(constT&)[T=int]据我所知,第一次调用f(e1)会导致可行的功能voidf(constint&)voidf(int&)从中选择第一个,因为没有删除const限定。第二次调用f(e2)导致类型推导/可行函数voidf(constint&);//T->intfromfirsttemplateoverloadvoidf(constint&);//T

c++ - std::move into static_pointer_cast:为什么 static_pointer_cast 没有右值引用重载?

假设我们有一个函数需要一个按值共享的指针。(在现实生活中的例子中,我通过右值引用获取它并将其转发给成员。)voidf(std::shared_ptrptr){...}但是我们只有一个指向基类的共享指针,所以我们使用static_pointer_cast:std::shared_ptrptr=std::make_shared();f(std::static_pointer_cast(ptr));第一个赋值(从临时构造ptr)是否触发了引用计数的原子递增和递减,或者共享指针是否被移动?(请注意,它正在向上转换。)在static_pointer_cast中有引用计数的原子增量。如果我们不再需

c# - 与 COM 中的 QueryInterface 或 C++ 中的 dynamic_cast 相比, "as"的成本是多少?

我仍在尝试将我深厚的旧知识从C/C++映射到我较浅的.Net知识。今天是时候在C#中使用“as”(隐含地“is”和cast)了。我对“as”的心理模型是,它是一个QueryInterface或dynamic_cast(一个带有指针参数的dynamic_cast,而不是引用,也就是说)对于C#。我的问题有两个方面:我的比较公平吗?与QueryInterface或dynamic_cast相比,“as”的相对成本是多少? 最佳答案 是的,比较是公平的,尤其是在处理指针时。这三个中的每一个要么成功并返回目标类型的非空指针,要么返回null。

c++ - 'auto' 关键字如何知道何时使用 const_iterator 匹配函数重载?

我了解thisquestion的内容但是当使用函数重载时,事情是如何工作的呢?例如在std::map中定义了以下方法:iteratorfind(constkey_type&k);const_iteratorfind(constkey_type&k)const;如何使用auto关键字来选择一个或另一个?以下内容对我来说似乎不正确:autoi=mymap.find(key);//callsthenon-constmethod?constautoi=mymap.find(key);//callstheconstmethod? 最佳答案 s

c++ - ‘const’ 错误之前的预期主表达式

请帮忙。我收到很多错误。sub2.cpp:在函数“intmain()”中:sub2.cpp:11:14:错误:从‘constchar*’到‘char’的无效转换[-fpermissive]sub2.cpp:12:14:错误:从‘constchar*’到‘char’的无效转换[-fpermissive]sub2.cpp:16:17:错误:'const'之前需要主表达式sub2.cpp:16:36:错误:'const'之前需要主表达式sub2.cpp:11:6:警告:未使用的变量“外部”[-Wunused-variable]sub2.cpp:12:6:警告:未使用的变量‘inner’[-W

c++ - 当底层 OpenGL 状态被修改时,我是否应该声明一个方法 const

下面的类封装了缓冲区的OpenGL名称,并提供了一些改变缓冲区状态的方法:classBufferObject{public:explicitBufferObject(GLenumtype);virtual~BufferObject();//somemethodsomittedvoiddataStore(GLsizeiptrsize,constGLvoid*data,intusage);void*mapBufferRange(GLintptroffset,GLsizeiptrlength,intaccessFlag);voidunmapBuffer()const;private:GLui

c++ - 使用对 const char * 的右值引用的重载解析

#includeusingnamespacestd;voidf(constchar*const&s){cout输出:rvaluervalue为什么输出不是“右值左值”? 最佳答案 字符串文字和s都不是指针(它们是数组),因此标准的相关部分是[conv.array]:Anlvalueorrvalueoftype"arrayofNT"or"arrayofunknownboundofT"canbeconvertedtoaprvalueoftype"pointertoT".Theresultisapointertothefirsteleme