草庐IT

const_cast-ing

全部标签

c++ - 为什么 "auto"将字符串声明为 const char* 而不是 std::string?

我制作了一个模板,其中添加了给定的数据。如果我这样使用它,编译器会将in_1和in_2声明为constchar*,并且代码不会编译。#includeusingnamespacestd;templateTaddstuff(Tpart_1,Tpart_2){return(part_1+part_2);}intmain(intargc,charconst*argv[]){autoin_1="Shut";autoin_2="up.";cout如果我声明in_1和in_2std::string,它就像一个魅力。为什么编译器不能(或没有)自动声明这些字符串std::string?

c++ - 返回指针 vector 的类中的 const 方法

假设我有这个类:#includeusingnamespacestd;classBag{vectoritems;public:voidaddItem(int*i){items.push_back(i);}constvectorgetItems()const{returnitems;}};问题是我想防止更改参数项中指针指向的值。但是使用从getItems返回的vector我可以更改指向的值。现在,为了解决这个问题,我可以将参数项声明为vector但是我也无法更改类(class)中的值(value)观。有没有其他方法可以保护值但仍然不在项目声明中使用const?

c++ - 对指向 const 对象的非常量指针的非常量引用

简单来说,我有一个简单的指针:int*a;现在,我想改变这个指针的值。我想在一个函数中执行此操作。函数保证,它不会改变指针指向的对象,但会改变指针本身。这就是为什么我希望这个函数采用这样的参数:非常量引用(因为指针的值将被更改)到非常量指针(指针本身可以更改)指向常量对象(函数保证,那个对象,该指针指向不会改变)。最简单的函数是:voidfunction(constint*&a){a=0;}但是当我尝试调用这个函数时:intmain(){int*a;function(a);return0;}编译器不高兴并说:invalidinitializationofnon-constreferen

c++ - std::upper_bound 在 const 成员函数中返回 const 迭代器

这是一个类,其中包含一些struct的boost::circular_buffer。我为包含的circular_buffer中的迭代器创建了一个typedef。我的问题是:当doWork函数被标记为const时,std::upper_bound的返回值与MyIterator类型,因为返回值具有boost::cb_details::const_traits。如果我从函数中删除const关键字,我所有的编译错误都会消失。要明确编译器错误是这样的:error:conversionfrom‘boost::cb_details::iterator::Sample,std::allocator::

c++ - floor 和 duration_cast 有什么区别?

所以在c++11Chrono图书馆提供,duration_cast:Computationsaredoneinthewidesttypeavailableandconverted,asifbystatic_cast,totheresulttypeonlywhenfinished和c++17的floor:ReturnsthegreatestdurationtrepresentableinToDurationthatislessorequaltod所以对于所有x这两个调用的结果是否相等:chrono::duration_cast(x)chrono::floor(x)

c++ - static_cast 的指针值

在当前标准草案(和C++17)中,this是关于static_castingvoid*的:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T”,whereTisanobjecttypeandcv2isthesamecv-qualificationas,orgreatercv-qualificationthan,cv1.IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAdoesnotsatisfytheali

c++ - "const T& operator[](size_type i)"中的 const 有什么用?

我在一本书http://www.acceleratedcpp.com/中发现了这个有趣的行-资源-第11章-Vec.h(我是一个std::vector翻版)而且我真的不明白这个版本的运算符有什么用。为什么要定义此运算符的两个版本(常量和非常量)?我什至试过了,在我看来,非常量版本一直被调用......你能解释一下吗?#include#include#include#includeusingnamespacestd;templateclassVec{public:typedefT*iterator;typedefconstT*const_iterator;typedefsize_tsiz

c++ - 地址、reinterpret_cast 和多重继承

任何人都可以解释以下代码的行为吗?为什么在第一种情况下我们有b=3,即b2==&d为真?为什么在案例2中没问题?b2和d的地址我打印出来了,它们是不一样的。#includeusingnamespacestd;classA{public:A():m_i(0){}protected:intm_i;};classB{public:B():m_d(0.0){}protected:doublem_d;};classC:publicA,publicB{public:C():m_c('a'){}private:charm_c;};intmain(){Cd;B*b2=&d;cout(b2)==rein

c++ - 检查 C++03 上是否为 const

如何在没有C++11的std::is_const的情况下检查对象是否为const?据我所知,我不应该const_cast声明const的对象 最佳答案 在cppreference上给出了C++11的is_const的示例实现。,它看起来像这样:templatestructis_const:false_type{};templatestructis_const:true_type{};如果你把这个定义放在你的C++03代码中,你也可以在那里使用is_const,如果你添加false_type和true_type(感谢mfonantin

c++ - 如何获得一个const限定的declval?

考虑以下代码:#include#include#includestructBase{intf()const;doublef();};structDerived:publicBase{template().f())>//ModifythisTg()const;};intmain(){constDerivedx;std::cout如何修改decltype(std::declval().f())这样它就会返回int而不是double?我试过了decltype(std::declval().f()但它不编译。 最佳答案 GCC4.8.1喜欢