草庐IT

const_cast-ed

全部标签

c++ - const_cast 在常量表达式中有效吗? (C++14, C++17)

a遇到的具体问题是编译器处理它的方式存在一些不一致。例如这段代码(https://godbolt.org/z/08Z-zi):constexprautovalue=1;static_assert(*const_cast(&value),"valueshouldbe1");使用GCC、Clang和MSVC编译良好,但使用英特尔C++编译器19.0.1失败并出现以下错误:error:expressionmusthaveaconstantvaluestatic_assert(*const_cast(&value),"valueshouldbe1");据我所知,标准并未明确声明常量表达式中不允

c++ - gcc 与 clang : noexcept parsed in unused template specialization when static casting

我正在尝试将函数指针静态转换为特定函数重载,但似乎clang仍会解析(未使用的)模板特化的noexcept语句,从而生成编译器错误。如果未使用相应的函数重载,GCC似乎并不关心noexcept。templatevoidfun(T)noexcept(T(1)){}voidfun(int){}voidfun(int*){}intmain(){inta;fun(&a);//callingworksfinefun(a);static_cast(&fun);//staticcastingdoesn't}https://godbolt.org/z/ixpl3f这里是哪个编译器出错了?当将函数指针转

c++ - 构造函数参数,仅当实例为 const 时才接受指向 const 的指针

假设我有一个类:classA{B*b;public:A(B*pb):b(pb){}}并且我确保A的const方法永远不会修改b指向的对象。这样当A的实例是const时,持有Bconst*是安全的。Bconstb;Aa(&b);//compileerrorAconstca(&b);//compileerrortoo.HowcanIallowthisone 最佳答案 这不可能——构造函数甚至无法告诉对象是否(将要)const,更不用说用信息控制重载决议了。标准解决方法(通常用于迭代器)是制作A一个模板(即使它只有两个专业)并使用A来处理

c++ - const 指向成员的指针是否默认指向一个 int?

在玩指向成员的指针时,我遇到了一种似乎有点不一致并且对我来说有点违反直觉的行为。考虑以下虚拟结构:structfoo{intx;doubled;};和以下main():intmain(){intfoo::*ptr=&foo::x;doublefoo::*ptr2=&foo::d;}这里我们没有任何异常-两个指向const成员的指针。代码编译正常。引起我注意的是,当我们添加const时,情况发生了一点变化。考虑以下代码:intmain(){//nointordoubleafterconstconstfoo::*ptr=&foo::x;}代码在GCC8.2.01上编译良好。请注意,我没有指

c++ - 带有转换运算符的类上的 static_cast

我刚刚遇到这种行为,我很难理解为什么这不起作用。enumclassTestEnum{Foo,Bar};classMyClass{public:operatorTestEnum(){returnm_enum;}TestEnumm_enum=TestEnum::Foo;}MyClasstheClass;intenumValue=static_cast(theClass);//doesnotwork,conversionoperatornotcalledintenumValue=static_cast(static_cast(theClass))//worksasexpected我知道编译器

c++ - boost::any_cast - 仅在隐式转换不可用时抛出?

我要boost::any_cast仅在any类型时抛出异常没有隐式转换为T.如果any的类型,正常行为似乎是抛出异常。不是T,不考虑隐式转换。例子:boost::anya=1;boost::any_cast(a);//Thissucceeds,andrightfullysoboost::any_cast(a);//Idon'twantthistothrowboost::any_cast(a);//Iwantthistothrow谁能告诉我是否有一种简单的方法来获得我想要的功能,或者更好的是给我一个充分的理由来说明为什么现有的行为是这样的? 最佳答案

c++ - const 重载的 operator[] 函数及其调用

我在类array中定义了两个版本的重载operator[]函数。ptr是指向array对象第一个元素的指针。int&array::operator[](intsub){returnptr[sub];}和intarray::operator[](intsub)const{returnptr[sub];}现在,如果我定义一个const对象integer1只能调用第二个函数......但是如果我创建一个非const对象然后调用如下:cout这里调用了哪个函数? 最佳答案 在您的第二个示例中,将调用非常量版本,因为不需要转换,不需要转换的调

c++ - ARM C++ - 如何将 const 成员放入闪存中?

我有这个代码classIO{public:IO(LPC_GPIO_TypeDef*port,intpin):_pin(pin),_port(port){};constint_pin;LPC_GPIO_TypeDef*const_port;voidtest(){LPC_GPIO0->FIOSET=0;}};IOled1(LPC_GPIO0,5);intmain(){led1.test();return0;}当我编译它时,我得到了textdatabssdechexfilename65608664298lpc17xx我希望const_port和_pin变量存储在闪存中,因为它们被标记为con

c++ - reinterpret_cast<int*>(char*) 与 static_cast<int*>(static_cast<void*>(char*)) - 使用哪个?

当你动态分配了一个char*类型的缓冲区并想将它转换为特定类型时,你是否应该使用类似的东西reinterpret_cast(char*)或者类似的东西static_cast(static_cast(char*))为什么?我个人很想使用后者,因为对我来说,它并不是真正的数据“重新解释”(而只是一种分配缓冲区的机械方式)而且它看起来不像是一个来源错误的方式可能与典型的reinterpret_cast相同,但这是正确的直觉吗? 最佳答案 AccordingtoDaveAbrahams,使用链式static_casts是强制指针类型的正确、

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_