草庐IT

const_cast-ing

全部标签

c++ - const_iterator<T> 和 iterator<const T> 有什么区别?

假设我正在实现一个集合,比如std::vector.我需要实现iterator和const_iterator,但一旦我做了iterator可以const_iterator不只是实现为iterator(其中T是集合中包含的类型)?肯定有一些原因导致这行不通,因为关于如何在实现iterator时重用代码存在一百万个问题。和const_iterator但他们都没有说“只需使用constT作为类型”。 最佳答案 std::iterator_traits::value_type应该是T对于const_iterator,但是constT1表示i

c++ - 这是 const_cast 未定义的行为吗?

我想知道以下是否是未定义的行为//Case1:int*p=0;intconst*q=*const_cast(&p);//Case2:(Ithinkthisisthesame)int*p=0;intconst*const*pp=&p;intconst*q=*pp;读取int*是否是未定义的行为,就好像它是intconst*一样?我认为这是未定义的行为,但我以前认为通常只添加const是安全的,所以我不确定。 最佳答案 资格方面,没问题。将每个表达式拆分为一个语句:int*p=0;//okint**addrp=&p;//okintcon

php - 为什么 PHP 不像 C++ 那样具有属性 'const'?

为什么PHP没有道德上等同于C++const?我认为这是PHP语言所缺乏的。有没有办法模拟与const对象或参数相同的属性? 最佳答案 C#也不做const事情,它是一种非常通用的语言。我是const的忠实粉丝,但我明白为什么脚本语言中的脚本往往不使用它们:脚本语言非常适合“裸奔穿过树林”,因为它“有趣”,并且可以非常快速地添加新功能,因为您没有强制执行严格的类型正确性,包括const。Perl、Python和Ruby使用标量是有原因的,传递/返回数组也是有原因的,因为最初“增长”系统非常容易。添加类型正确性(包括使用const)可

c++ - 为什么我不能使类内初始化 `const const std::string` 成为静态成员

我有以下工作代码:#include#includeclassA{public:conststd::stringtest="42";//staticconststd::stringtest="42";//fails};intmain(void){Aa;std::cout为什么不能使测试成为staticconst有充分的理由吗?我确实了解在c++11之前它受到标准的约束。我认为c++11引入了类内初始化以使其更友好一些。很长一段时间以来,我也没有这样的语义可用于整数类型。当然,它适用于conststd::stringA::test="42";形式的类外初始化我猜想,如果您可以使它成为非静态

c++ - 函数模板修改用顶级 const 声明的参数 : clang bug?

下面的代码可以在ArchLinux上的clang3.8.1-1上正确编译。这是clang错误吗?gcc对此发出正确的警告/错误。templatestructBugReproducer{usingsize_type=typenameT::size_type;intbug1(size_typecount);intbug2(size_typecount)const;staticintbug3(size_typecount);};templateintBugReproducer::bug1(size_typeconstcount){//thisisabug.mustbenotallowedco

c++ - 不一致的警告 "conversion from ' const unsigned char' to 'const float' requires a narrowing conversion”

VisualC++2017和gcc5.4产生conversionfrom'constunsignedchar'to'constfloat'requiresanarrowingconversion警告LineB但没有此代码段中的A行:#includeintmain(){constunsignedcharp=13;constfloatq=p;//LineAstd::cout这个警告有效吗?为什么LineB的处理方式与LineA不同? 最佳答案 警告有效,来自C++11narrowingconversions在aggregateiniti

C++ : How to write a const_iterator?

我用迭代器编写了自己的容器模板。如何实现const_iterator?templateclassmy_container{private:...public:my_container():...{}~my_container(){}classiterator:publicstd::iterator{public:... 最佳答案 唯一的区别应该是,当您取消引用const迭代器时,您得到的是const引用,而不是对容器中对象的引用。 关于C++:Howtowriteaconst_itera

c++ - 不使用 reinterpret_cast 读取二进制数据

只是因为在我编写读取二进制STL文件的程序之前,我从未读取过二进制文件。我使用带有char*参数的ifstream读取成员。为了将我的结构转换为char*,我使用了reinterpret_cast。但据我所知,我读过的每本关于C++的书都说过类似“除非你必须使用,否则不要使用reinterpret_cast”之类的话。有什么更好的方法来读取二进制数据,不一定是直接读取,但最终读取到一个结构中并且不需要reinterpret_cast?主要功能:std::ifstreamin(cmdline[1].c_str(),std::ios::binary);in.seekg(80,std::if

c++ - 为什么 BOOST_FOREACH 无法处理 const boost::ptr_map?

voidmain(){typedefboost::ptr_mapMyMap;//typedefstd::mapMyMap;//incontrastwithstdtypeitworksMyMapmymap;mymap[1]=newchar('a');mymap[2]=newchar('b');mymap[3]=newchar('c');BOOST_FOREACH(MyMap::value_typevalue,mymap){std::cout以下错误消息来自GCC在第二个BOOST_FOREACHerror:conversionfrom'boost::ptr_container_detai

c++ - 几乎 pod 数据的 reinterpret_cast(布局兼容性是否足够)

我正在尝试了解static_cast和reinterpret_cast。如果我是正确的,标准(9.2.18)表示pod数据的reinterpret_cast是安全的:ApointertoaPOD-structobject,suitablyconvertedusingareinterpret_cast,pointstoitsinitialmember(orifthatmemberisabit-field,thentotheunitinwhichitresides)andviceversa.[Note:TheremightthereforebeunnamedpaddingwithinaPO