草庐IT

c++ - `reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(&ch) + 1) == &ch +1` 有保证吗?

我正在编写与对齐相关的代码,如果给定的指针正确对齐,却没有标准的功能测试,这让我感到非常惊讶。似乎互联网上的大多数代码都使用(long)ptr或reinterpret_cast(ptr)测试对齐,我也使用了它们,但我想知道使用指向整数类型的强制转换指针是否符合标准。这里是否有任何系统触发断言?charch[2];assert(reinterpret_cast(reinterpret_cast(&ch[0])+1)==&ch[1]); 最佳答案 回答题目中的问题:没有。反例:在旧的Pr1me微型计算机上,一个普通指针是两个16位字。第

c++ - reinterpret_cast 指向基类的指针的 vector 的 vector

考虑以下代码#include#include#include#includestructBase{intx;Base(intx):x(x){}};structDerived:publicBase{inty,z;Derived(intx):Base(x),y(x+1),z(x+2){}};voidupdate(conststd::vector>&elements){for(constautoelem:elements){std::coutx>elements(4);{intctr=0;std::generate(begin(elements),end(elements),[&ctr]()

显式类型转换(C 风格转换)的 C++ 转换符号和 static_cast 的多种解释

Paragraph4of[expr.cast](在撰写本文时可用的最新C++标准草案中)描述了C样式转换的行为如下:Theconversionsperformedbyaconst_­cast,astatic_­cast,astatic_­castfollowedbyaconst_­cast,areinterpret_­cast,orareinterpret_­castfollowedbyaconst_­cast,canbeperformedusingthecastnotationofexplicittypeconversion.Thesamesemanticrestrictionsan

c++ - 智能指针 : cast between base and derived classes

假设你有这样一个函数:SmartPtrdoSomething(SmartPtra);像这样的类:classA{}classB:publicA{}现在我这样做:SmartPtrfoo=newB();doSomething(foo);现在,我想取回一个SmartPtr来自doSomething的对象.SmartPtrb=doSomething(foo);这可能吗?我需要做什么样的选角?现在,我刚发现一些我认为丑陋的东西:B*b=(B*)doSomething().get()重要说明:我无权访问SmartPtr和doSomething()代码。 最佳答案

c++ - 比 C++ 中的 dynamic_cast 更好的解决方案

我有一个为我的项目设计的类层次结构,但我不确定如何实现它的一部分。这是类层次结构:classShape{};classColored{//Onlypurevirtualfunctions};classSquare:publicShape{};classCircle:publicShape{};classColoredSquare:publicSquare,publicColored{};classColoredCircle:publicCircle,publicColored{};在我的部分项目中,我有一个不同类型形状的std::vector。不过,为了运行算法,我需要将它们放在彩色对

c++ - C 样式转换和 C++ static_cast 到指针的引用

这不是C++11我对微软的第三个参数感兴趣CMapStringToOb::GetNextAssoc,其定义如下:voidGetNextAssoc(POSITION&rNextPosition,CString&rKey,CObject*&rValue)const;然后我得到了以下用于测试的简单代码:两个好的案例和一个编译器错误的案例。classCMyObject:publicCObject//inordertouseCMapStringToOb{public:CMyObject(CStringname_):name(name_){}voidSayHello(){TRACE(_T("hel

c++ - static_cast 带走了常量

据我所知(以及本主题:Whenshouldstatic_cast,dynamic_cast,const_castandreinterpret_castbeused?)const_cast是唯一应该能够消除变量常量性的强制转换。然而,当弄乱clang-6.0和g++5.4.0时,我偶然发现了一种与上述相矛盾的行为。看起来static_cast做的工作完全一样。这些主要函数在两个编译器中给出了完全相同的结果:测试类定义structBase{Base(){std::cout使用const_castintmain(void){std::cout(b).no_const();std::cout使

c++ - dynamic_cast 与动态库边界

我正在阅读“C++GUIProgrammingwithQt4”,在那里我发现了以下语句UnliketheStandardC++dynamic_cast(),Qt’sqobject_cast()workscorrectlyacrossdynamiclibraryboundaries.Qt的官方文档中也有类似的说法,网址为https://doc.qt.io/qt-5/qobject.html#qobject_cast这是什么意思?我们不能在C++中使用dynamic_cast的确切位置是什么?那么虚函数呢?将它们与动态链接库一起使用是否安全? 最佳答案

【C++入门到精通】C++类型的转换 | static_cast | reinterpret_cast | const_cast | dynamic_cast [ C++入门 ]

阅读导航引言一、强制转换(集成C语言的语法)二、static_cast操作符1.操作符介绍2.使用示例(1)基本类型之间的转换(2)类型之间的隐式转换(3)类指针和引用之间的转换三、reinterpret_cast操作符1.操作符介绍2.使用示例(1)将指针转换为整数(2)将整数转换为指针(3)将指向基类的指针转换为指向派生类的指针(4)将指向不同类型的指针进行转换四、const_cast操作符1.操作符介绍2.使用示例(1)移除常量性以修改对象的值(2)在函数中移除常量性以调用非常量版本的成员函数(3)移除常量性以进行底层操作五、dynamic_cast操作符1.操作符介绍2.使用示例(1)

c++ - 为什么 const_cast 需要说明你要转换到什么?

根据标准(§5.2.11),const_cast会丢弃cv限定符(const或volatile)。这是一个简单的例子。首先你声明两个函数接受一个指针和一个引用:classBar{...};voidfoo_ptr(Bar*);voidfoo_ref(Bar&);然后你创建一个常量引用:Barb;constBar&cb=b;然后您可以使用适当的const_cast调用任一函数:foo_ptr(const_cast(&cb));foo_ref(const_cast(cb));这是我的问题:既然const_cast不能做其他类型转换的设计目的,那么您要转换成什么不是很明显吗?换句话说,为什么语