草庐IT

cast_sender

全部标签

c++ - 如何将 dynamic_cast 与 for_each 一起使用

我有以下代码:vector::iteratoritr=vec.begin();for(;itr!=vec.end();++itr){C2*c=dynamic_cast(*itr);c->f();}我想知道是否可以使用一行for_each来替换它。我尝试了以下方法:for_each(vec.begin(),vec.end(),bind2nd(mem_fun(&C2::f),dynamic_cast));但是我得到一个编译错误,expectedunqualified-idbefore'dynamic_cast'那正确的应该是什么?[编辑]我不能使用c++11。看来我必须定义一个额外的仿函数

c++ - 在不同限定的结构成员上使用 reinterpret_cast 是否安全?

我查看了以下相关的问题,但似乎没有一个能解决我的确切问题:one,two,three.我正在编写一个集合,其中的元素(键值对)与一些簿记信息一起存储:structElement{Keykey;Valuevalue;intflags;};std::vectorelements;(为简单起见,假设Key和Value都是标准布局类型。该集合无论如何都不会与任何其他类型一起使用。)为了支持基于迭代器的访问,我编写了覆盖operator->和operator*的迭代器,以向用户返回一个指针和一个引用,分别为键值对。但是,由于集合的性质,永远不允许用户更改返回的key。为此,我声明了一个KeyVa

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++样式转换的其他问题,但我仍然不确定这种情况下正确的方法是什么(我认为