草庐IT

c++ - 使用 const_cast<> 时未定义的行为在哪里?

如果我这样做:constchar*const_str="Somestring";char*str=const_cast(const_str);//(1)str[0]="P";//(2)未定义的行为到底在哪里(哪一行)?我一直在SO上搜索很多,但没有找到任何明确和准确的答案(或者至少,我无法理解)。另外相关:如果我使用提供这种功能的外部库://Thedocumentationstatesthatstrwillneverbemodified,justread.voidread_string(char*str);是否可以这样写:std::stringstr="Mystring";read_s

c++ - 将 std::make_tuple 与重载函数一起使用时如何避免 static_cast

g++说error:toomanyargumentstofunction'constexprstd::tuple如果我在std::make_tuple调用中省略了static_cast#includetypedefint(*func_t)();intnumber(){return2;}doublenumber(boola){return1.2;}intmain(){//Withastatic_castitcompileswithoutanyerror//std::tupletup=std::make_tuple(static_cast(number));std::tupletup=st

c++ - 使用 static_cast 处理混合(基础和派生)对象的 vector 是否存在性能风险? (又名 "it this a dumb idea?")

给定基类gameObject和派生类animatedGameObject,我认为将它们的所有实例存储在std::vector。如果vectorGameObjects声明为gameObject*的基类型,则派生对象实例需要强制转换。例子:vectorGameObjects;gameObjectA*=newgameObject(...init...);animatedGameObjectB*=newanimatedGameObject(...init...);GameObjects.push_back(A);GameObjects.push_back(B);//toaccesstheani

c++ - (void*) 和 (void(*)(argument type)) cast 之间有什么区别?

这个问题在这里已经有了答案:WhyarefunctionpointersanddatapointersincompatibleinC/C++?(14个答案)关闭7年前。voidfuncPtr(inta);intmain(){intk=1;void(*funcPtr2)(int);funcPtr2=(void*)(funcPtr);//funcPtr2=(void(*)(int))(funcPtr);(*funcPtr2)(k);return0;}voidfuncPtr(inta){printf("%d",a);}函数指针类型转换中(void*)和(void(*)(argumenttyp

c++ - duration_cast 怎么轮

如果我转换为更粗略的时间单位(例如std::chrono::minutes为std::chrono::hours),duration_cast将如何圆?例如,如果转换为std::chrono::hours,std::chrono::minutes(91)会变成什么值?2小时,1小时? 最佳答案 duration_cast总是向零舍入。IE。正值向下舍入,负值向上舍入。有关其他舍入选项,请参阅:http://howardhinnant.github.io/duration_io/chrono_util.htmlfloor、ceil和r

c++ - static_cast 转换构造函数 vs 转换运算符

这个问题在这里已经有了答案:Shouldn'tthiscodethrowanambiguousconversionerror?(1个回答)关闭6年前。看完this我尝试使用static_cast进行此类转换:classA;classB{public:B(){}B(constA&)//conversionconstructor{cout(a);return0;}我预计它不会编译,因为构造函数和运算符都具有相同的c-v资格。然而,它编译成功,static_cast调用构造函数而不是运算符。为什么?(使用带有pedantic和Wall标志的gcc4.8.1编译)

c++ - sibling 的 dynamic_cast 的用例是什么?

我正在阅读ScottMeyers的《更有效的C++》。教化!Item2提到dynamic_cast不仅可以用于向下转换,还可以用于兄弟转换。任何人都可以提供一个(合理的)非人为的例子来说明它对sibling的用法吗?这个愚蠢的测试按它应该打印0,但我无法想象任何用于此类转换的应用程序。#includeusingnamespacestd;classB{public:virtual~B(){}};classD1:publicB{};classD2:publicB{};intmain(){B*pb=newD1;D2*pd2=dynamic_cast(pb);cout

c++ - 使用 C++ 样式转换从 Void* 转换为 TYPE*​​ : static_cast or reinterpret_cast

因此,如果您从Void*转换为Type*或从Type*转换为Void*,您应该使用:voidfunc(void*p){Params*params=static_cast(p);}或voidfunc(void*p){Params*params=reinterpret_cast(p);}对我来说static_cast似乎更正确,但我已经看到两者用于同一目的。此外,转换的方向是否重要。即我是否仍应将static_cast用于:_beginthread(func,0,static_cast(params)我已经阅读了关于C++样式转换的其他问题,但我仍然不确定这种情况下正确的方法是什么(我认为

c++ - 从 Derived* 到 void* 到 Base* 的 static_cast

我想将指向派生类成员的指针转换为void*,然后从那里转换为基类的指针,如下例所示:#includeclassBase{public:voidfunction1(){std::cout(&d);Base*baseptr=static_cast(ptr);baseptr->function1();baseptr->function2();}这会编译并给出所需的结果(分别打印1和2),但它能保证工作吗?我在这里找到的static_cast的描述:http://en.cppreference.com/w/cpp/language/static_cast只提到转换为void*并返回到指向相同类

C++:Cast 运算符重载和引用

C++允许通过创建operatorT()来重载类型转换,其中T是我们要转换为的类型。现在,这个功能如何与引用一起发挥作用?例如:structY{inti;};structX{Yy;operatorY()const{returny;}};在这里,我们可以将X转换为Y,这将简单地返回包含的Y。但是,如果我们想转换为Y引用怎么办?例如,C++允许我们这样做:structX{Yy;operatorY&(){returny;}operatorconstY&()const{returny;}};现在,我们可以将X转换为Y引用或const引用(这也适用于constX)。第一个和第二个例子在语义上有什