草庐IT

production_cast

全部标签

c++ - reinterpret_cast 会导致未定义的行为吗?

我有一个类模板A其中包含一个指针容器(T*):templateclassA{public://...private:std::vectordata;};还有一堆函数,比如:voidf(constA&);voidg(constA&);通过来自A的转换调用这些函数是否可以?至A?Aa;...auto&ac=reinterpret_cast&>(a);f(ac);我很确定这段代码有未定义的行为。在现实生活中使用这种转换是否危险? 最佳答案 尽管reinterpret_cast本身可能是未指定的行为,但在完成转换后尝试访问参数是未定义的行为

c++ - 将_cast 重新解释为 void 是否合法*

我在看https://en.cppreference.com/w/cpp/language/reinterpret_cast我注意到它指定了我们始终可以转换为的合法类型:字节*char*unsignedchar*但是我没有在列表中看到void*。这是疏忽吗?我的用例需要reinterpret_cast,因为我正在从int**转换为void*。我最终将从void*转换回int**。 最佳答案 这些类型不受严格的别名规则约束。这并不意味着它们是您可以与reinterpret_cast一起使用的唯一类型.在将对象指针转换为另一种对象指针类

c++ - const_cast 是否会导致实际的代码排放?

const_cast真的只是告诉编译器“停止提示,将其视为非常量指针”的一种方式吗?有没有const_cast本身被翻译成实际机器代码的情况? 最佳答案 不,它只是在编译时删除了const属性。 关于c++-const_cast是否会导致实际的代码排放?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/759315/

c++ - 当编译器看不到可以完成转换的可能类型时,是否允许编译器优化 volatile 指针的 dynamic_cast?

看看这个小片段:structA{virtual~A(){}};structB{};boolfn(){A*volatilea=newA;returndynamic_cast(a);}是否允许编译器完全删除dynamic_cast,并将dynamic_cast转换为简单的nullptr;?这个问题的原因是这个answer.注意事项:假定volatile意味着编译器不能假定任何有关a的信息,因为它是易变的。这是一个question为什么。dynamic_cast可能不允许被删除的事实是程序中某处可能有一个类型,它派生自A和B。 最佳答案

c++ - reinterpret_cast 可以更改对象表示吗?

我对reinterpret_cast的心理模型一直是,将表达式的位序列视为不同类型,并且cppreference(注意:这不是C++标准的引用)似乎同意这一点:Unlikestatic_cast,butlikeconst_cast,thereinterpret_castexpressiondoesnotcompiletoanyCPUinstructions.Itispurelyacompilerdirectivewhichinstructsthecompilertotreatthesequenceofbits(objectrepresentation)ofexpressionasifi

c++ - 我可以通过 reinterpret_cast 将 int 的空指针转换为 long 类型吗

int*pt=0;longi=reinterpret_cast(pt);我保证为0吗?这是明确定义的还是实现定义的?我检查了c++标准,但它只说明了Apointertoadataobjectortoafunction(butnotapointertomember)canbeconvertedtoanyintegertypelargeenoughtocontainit.在这种情况下,pt不指向任何数据对象。该规则适用于这种情况吗? 最佳答案 否,i不一定是任何值。结果是实现定义的。†在C++中,指针的表示是实现定义的,包括空指针的表示

c++ - 为什么我不能使用 static_cast<int&> 将整数引用参数传递给 C++ 中的函数?

我在C++程序中有一个枚举参数,我需要使用一个通过参数返回值的函数来获取它。我首先将其声明为int,但在代码审查时被要求将其键入为枚举(ControlSource)。我这样做了,但它破坏了Get()函数——我注意到C风格的转换为int&解决了这个问题,但是当我第一次尝试用static_cast修复它时,它没有编译。为什么会这样,为什么当eTimeSource是一个int时根本不需要强制转换来通过引用传递整数?//GetCuePropertyValuesignatureis(intcueId,intpropertyId,int&value);ControlSourceeTimeSourc

c++ - const_cast 的未定义行为

我希望有人能准确阐明C++中未定义行为的含义。给定以下类定义:classFoo{public:explicitFoo(intValue):m_Int(Value){}voidSetValue(intValue){m_Int=Value;}private:Foo(constFoo&rhs);constFoo&operator=(constFoo&rhs);private:intm_Int;};如果我理解正确,下面代码中指向引用和指针的两个const_casts将删除Foo类型的原始对象的常量性,但是通过指针或引用将导致未定义的行为。intmain(){constFooMyConstFoo

c++ - 处理不相关类型的 Dynamic Cast

#includeusingnamespacestd;classX{public:virtualvoidf(){}};classY{public:virtualvoidg(){}};intmain(){X*x=newX();Y*y=dynamic_cast(x);//A//Y*y=static_cast(x);//BcoutA编译而B不编译。我明白为什么B没有被编译但是为什么A被编译虽然X和Y是完全不相关的类型? 最佳答案 这就是为什么dynamic_cast在不相关的类型之间被允许:classX{public:virtualvoid

c++ - reinterpret_cast 什么时候修改位?

来自C++标准:5.2.10.3Themappingperformedbyreinterpret_castmight,ormightnot,producearepresentationdifferentfromtheoriginalvalue.我在这个网站接受过培训,相信并重复这一点。(即使可能只是琐事)。从float*到int*的reinterpret_cast被允许产生不同的位模式。唯一的保证是reinterpret_cast将结果返回到float*将产生原始位模式。我的问题:这会发生吗?是否存在实际reinterpret_cast为不同位模式的现有真实平台或CPU或编译器?如果不