草庐IT

my_numeric_cast

全部标签

c++ - dynamic_cast 困惑

我放弃了这个...$5.2.7/2-"IfTisapointertype,vshallbeanrvalueofapointertocompleteclasstype,andtheresultisanrvalueoftypeT.IfTisareferencetype,vshallbeanlvalueofacompleteclasstype,andtheresultisanlvalueofthetypereferredtobyT."根据上述,下面的代码应该是合式的。structA{};structB:A{};intmain(){Bb;Aa,&ar1=b;B&rb1=dynamic_cast

c++ - std::numeric_limits::is_exact ...什么是可用的定义?

在我解释时,numeric_limits::is_exact的MSDN'sdefinition几乎总是错误的:[all]calculationsdoneon[this]typearefreeofroundingerrors.IBM'sdefinition几乎总是正确的:(或循环定义,具体取决于您如何阅读)atypethathasexactrepresentationsforallitsvalues我确定的是,我可以将2既存储在double中,又存储在long中,并且它们都将被精确表示。然后,我可以将它们都除以10,而它们都不能精确地保存数学结果。给定任何数字数据类型T,定义std::n

c++ - const_cast<const Type*> 有用吗?

最近我发现一段C++代码可以有效地执行以下操作:char*pointer=...;constchar*constPointer=const_cast(pointer);显然作者认为const_cast表示“添加常量”,但实际上const也可以隐式添加:constchar*constPointer=pointer;在任何情况下我真的必须const_cast到一个指向常量的指针(const_cast,如上例)? 最佳答案 你有2个重载并且你想强制执行const一个。当您根据另一个来调用一个时,通常就是这种情况。classA{public

c# - 与 C++ 的 dynamic_cast 等效的 C# 是什么?

此C++代码检查o是否为Node*,如果是,则调用d上的方法。if(Node*d=dynamic_cast(o))d->do_it();用C#编写等效项的最短和/或最有效的方法是什么? 最佳答案 假设Node是一个class然后执行以下操作Noded=oasNode;if(d!=null){d.do_it();}如果它是一个struct那么试试这个if(oisNode){((Node)o).do_it();} 关于c#-与C++的dynamic_cast等效的C#是什么?,我们在Stac

C++ dynamic_cast - 多态要求和向下转型

在下面的代码中,同时构造obj在情况1中,我们构造一个derived类对象也是,但其成员函数无法访问obj.因此,在向下转换时(即情况2),使用obj作为来源,我们有构建的derived已经在里面了。为什么obj需要多态吗?如果我上面的描述让你感到困惑,为什么不obj向上转型时需要多态,但向下转型时确实在使用dynamic_cast时需要多态?classbase{public:base(){cout(newderived);//case1:explicitlyupcastingderived*OBJ=dynamic_cast(obj);//case2:error

C++ dynamic_cast 转发声明的类模板编译,但它安全吗?

以下代码在(GCC和clang)中编译并给出预期的结果:templatestructDerived;structBase{templatevoidfoo(T*constt){dynamic_cast*const>(this)->bar(t);}};templatestructDerived:Base{voidbar(Tconst*)const{}};代码将对Base中的foo的调用分派(dispatch)到Derived中的bar。作为引用,以下代码无法编译:structDerived2;structBase2{templatevoidfoo(T*constt){dynamic_cas

c++ - 为什么捕获 std::bad_cast 在 FreeBSD 9 上不起作用?

考虑这段代码(badcast.cpp):#include#include#includeclassfoo{public:virtual~foo(){}};classbar:publicfoo{public:intval;bar():val(123){}};staticvoidcast_test(constfoo&f){try{constbar&b=dynamic_cast(f);printf("%d\n",b.val);}catch(conststd::bad_cast&){printf("badcast\n");}}intmain(){foof;cast_test(f);return

c++ - g++ 上 constexpr 上下文中成员指针的 static_cast

我在使用static_cast在constexpr上下文中向上转换成员指针时遇到了g++问题。请参见代码示例。在使用g++6.3和7.0版进行编译时,会出现编译错误,指出reinterpret_cast不是常量表达式。虽然clang4.0版没有给出错误,但我认为这是正确的,因为这里没有reinterpret_cast。这是g++或clang中的错误吗?什么是正确的行为?structBase{};structDerived:Base{inti;};structPtr{constexprPtr(intDerived::*p):p(static_cast(p)){}intBase::*p;}

c++ - 将 reinterpret_cast 的派生类指针转换为基类指针未定义行为吗?

看一个简单的例子:structBase{/*somevirtualfunctionshere*/};structA:Base{/*members,overriddenvirtualfunctions*/};structB:Base{/*members,overriddenvirtualfunctions*/};voidfn(){Aa;Base*base=&a;B*b=reinterpret_cast(base);Base*x=b;//usexhere,callvirtualfunctionsonit}这个小片段是否有未定义的行为?reinterpret_cast定义良好,它返回base

c++ - C++ 类 std::numeric_limits 中的字段与方法

为什么在C++的模板类std::numeric_limits中,digits(和其他)被定义为该类的一个(静态常量)字段,但是min()和max()是方法,因为这些方法只返回一个垃圾值?提前致谢。 最佳答案 不允许在类主体中初始化非整型常量(例如:float)。在C++11中,声明更改为...staticconstexprTmin()noexcept;staticconstexprTmax()noexcept;...我认为,为了保持与C++98的兼容性,保留了函数。例子:structX{//IllegalinC++98andC++1