草庐IT

const_cast-ed

全部标签

c++ - 将 zlib 与 const 数据一起使用

要使用zlib压缩/解压缩数据,首先我需要设置一个名为z_stream的结构.z_stream有两个非常量指针叫做next_in和next_out.如果我想做这样的功能:voidungzip(std::vector&dst,conststd::vector&src){z_streamstrm;//morecode}和其他类似的voidgzip(std::vector&dst,conststd::vector&src);我该怎么办?在本地复制srcstd::vectorstd::vectortmp(src);并像这样将其用作源或设置指针,strm.next_in=const_cast(&

c++ - 为什么别名 int& 允许创建非函数类型 const (int&)?

通常在使用常量引用时会出现编译器错误,但在使用别名或使用模板时不会。为什么会这样?inta=5;usingmy_t=int&;my_tconstb=a;//#1OKint&constc=a;//#2Compilererror当运行最新的clang编译器(x86-64clang(实验性P1144))时,#1给我警告:[x86-64clang(experimentalP1144)#1]warning:'const'qualifieronreferencetype'my_t'(aka'int&')hasnoeffect[-Wignored-qualifiers]#2给出了错误:[x86-64

c++ - 将返回的迭代器转换为 const

我有以下for我的代码中的语句:for(autoIter=Target.begin(),IterEnd=std::stable_partition(Target.begin(),Target.end(),Check);Iter!=IterEnd;++Iter){/*loopstatement*/}重点是循环不会修改容器的元素,因此将迭代器声明为const_iterator是有意义的.我可以轻松解决第一次使用cbegin()的问题,但第二个更复杂。我不能申报cbegin()和cend()里面stable_partition,因为当然stable_partition需求nonconst_i

c++ - 这不是 The C++ Programming Language(4 ed) 一书中的错误吗?

我最近正在阅读这本厚厚的手册,以便对C++有更清晰和更深入的理解,我偶然发现了第8.4.2节(第222页)中的这段代码,我认为这是一个错误。代码如下:enumTraffic_light{red,yellow,green};enumWarning{green,yellow,orange,red};//firealertlevels//error:twodefinitionsofyellow(tothesamevalue)//error:twodefinitionsofred(todifferentvalues)Warninga1=7;//error:noint->Warningconve

c++ - 在 C++ 中添加 const-ness

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Istheresomeninjatricktomakeavariableconstantafteritsdeclaration?考虑以下最小示例:voidMutateData(std::string&);intmain(){std::stringdata="somethingthatmakessensetohumans.";::MutateData(data);//Mutates'data'--e.g.,onlychangestheorderofthecharacters.//Atthispoint,'dat

c++ - 我的代码能否使用 'T' 或 'const T &' 特化,以可用者为准?

我们正在与一个内部图书馆合作,其中有一个StringBuilder用于转换VariableValue列表的类对象变成一个字符串。VariableValue对象可以从任意类型构造(通过专门化convertVariable模板函数)。这是描述场景的代码:structVariableValue{//Constructa'VariableValue'object,avarianttypewhichcanrepresentvaluesof//oneoffourtypes:string,number(integer),booleanandfloatingpoint.explicitVariable

C++和const reference to temporary binding问题(在C++0X中实现D语言传值传引用规则)

我想知道可以在多大程度上模仿C++中按值传递和按引用传递规则的D语言规则。有关背景,请参阅以下两个引用资料(主要是Alexandrescu):http://bartoszmilewski.wordpress.com/category/d-programming-language/page/2/和http://groups.google.com/group/comp.std.c++/msg/303e3bf2407a7609?其中一个关键区别是,在D中,const引用不绑定(bind)(作为非const引用)到临时对象。但是,我不知道有什么方法可以定义泛型类X,从而导致以下代码无法编译:v

c++ - 为什么 Qt 对 void* 使用 reinterpret_cast 而不是 static_cast?

您可以使用static_cast将任何指向T的指针转换为void*或将其转换为void*,为什么Qt使用reinterpret_cast?intSOME_OBJECT::qt_metacall(QMetaObject::Call_c,int_id,void**_a){_id=QMainWindow::qt_metacall(_c,_id,_a);if(_id(_a[1])));break;default:;}_id-=1;}return_id;} 最佳答案 老实说,我也一直没弄明白。void**结构的创建方式相同,只需将int*转换

C++ const 与 std::pair 的正确性

我正在维护一个容器类,其接口(interface)类似于std::map/std::unordered_map.接口(interface)声明存储std::pair(即value_type是什么)。然而,在内部,该实现存储了一个排序的std::pair数组。.当前的实现使用reinterpret_cast实现迭代器。我的问题是,是否有更好的选择?开始存储std::pair的数组不可能,因为实现需要复制数组中的元素来实现插入和删除。它执行此操作的方法之一是使用std::sort.编辑虽然我相信reinterpret_cast调用未定义的行为(或定义的实现?)我还没有遇到过这样不起作用的编

c++ - 具有 const 不可复制元素的元组

我需要创建一个std::tuple其中XYZ不可复制。这样的事情甚至可能吗?我当前的代码autotest()->std::tuple{returnstd::make_tuple(XYZ());}在VisualStudio2010中导致C2248...我发现这很可疑,因为我正在构建具有R值的元组,所以我假设移动构造会启动... 最佳答案 您的问题是该元素既不可复制又const.constXYZ元素表现为constXYZ成员;通过5.2.5p4访问constXYZxvalue上的元素将产生具有unioncv资格的xvalue,即具有有效