草庐IT

cast_sender

全部标签

c++ - 重载 static_cast 的调用不明确

我有一些这样的代码structB{B(){}B(intv){}};structA{operatorint()const{return1;}operatorB()const{returnB();}};intmain(){Aa;static_cast(a);//Errorherea.operatorB();//ThisisOKreturn0;}会产生这样的编译错误:main.cpp:Infunction‘intmain()’:main.cpp:16:21:error:callofoverloaded‘B(A&)’isambiguousstatic_cast(a);^main.cpp:4:5

c++ - 符合标准的编译器可以拒绝包含来自非多态类型的 dynamic_cast downcast 的代码吗?

这个问题的灵感来自评论here.考虑以下代码片段:structX{};//novirtualmembersstructY:X{};//mayormaynothavevirtualmembers,doesn'tmatterY*func(X*x){returndynamic_cast(x);}一些人建议他们的编译器会拒绝func的正文.但是,在我看来,这是否由标准定义取决于x的运行时值。.来自第5.2.7节([expr.dynamic.cast]):Theresultoftheexpressiondynamic_cast(v)istheresultofconvertingtheexpres

c++ - 你能合法地将dynamic_cast转换为多态类的非多态基类吗

在thisanswer,出现了以下场景:#includestructA{};structB{virtual~B(){}};structAA{};templatestructC:A,T{};intmain(){B*b=newC;AA*aa=newC;assert(dynamic_cast(b));assert(dynamic_cast(aa));//thislinedoesn'tcompile,asexpected}在g++4.8.4(Ubuntu)上,它编译并且断言通过。我的问题是,这真的合法吗?我觉得您根本不应该将dynamic_cast转换为非多态类,但我坦率地承认,我不是这里发生

c++ - void* 与 static_cast 对比 intptr_t 与 reinterpret_cast

我想知道两种不同类型的非常特殊类型的转换表之间是否存在特定的、基于标准的差异。特别是,给定:类型T和变量T*object是:intptr_topaque=reinterpret_cast(object);T*result=reinterpret_cast(opaque);相当于:void*opaque=static_cast(object);T*result=static_cast(opaque);我只关心result,它是否保证是相同的值,相当于任何类型T的原始object?我不在乎中间opaque有什么位模式,因为我相信标准技术上允许它们在每种情况下都不同(尽管没有理智的编译器会产

c++ - 使用boost程序选项时如何解决 "boost::bad_any_cast: failed conversion using boost::any_cast"?

//Usingboostprogramoptionstoreadcommandlineandconfigfiledata#includeusingnamespacestd;usingnamespaceboost;namespacepo=boost::program_options;intmain(intargc,char*argv[]){po::options_descriptionconfig("Configuration");config.add_options()("IPAddress,i","IPAddress")("Port,p","Port");po::variables_

c++ - 为什么 std::forward 返回 static_cast<T&&> 而不是 static_cast<T>?

让我们有一个名为Y的重载函数:voidY(int&lvalue){cout现在,让我们定义一个类似于std::forward的模板函数templatevoidf(T&&x){Y(static_cast(x));//Usingstatic_cast(x)likeinstd::forward}现在看看main()intmain(){inti=10;f(i);//lvalue>>T=int&f(10);//rvalue>>T=int&&}正如预期的那样,输出是lvalue!rvalue!现在回到模板函数f()并替换static_cast(x)与static_cast(x).让我们看看输出:l

C++ 类型转换 : cast a pointer from void pointer to class pointer

如何将指向void对象的指针转换为类对象? 最佳答案 使用static_cast。请注意,只有当指针确实指向指定类型的对象时,您才必须这样做;也就是说,指向void的指针的值取自指向此类对象的指针。thing*p=whatever();//pointertoobjectvoid*pv=p;//pointertovoidthing*p2=static_cast(pv);//pointertothesameobject如果您发现自己需要这样做,您可能需要重新考虑您的设计。你放弃了类型安全,让编写无效代码变得容易:something_el

c++ - 用 static_cast<int> 舍入?

我觉得问这个真的很傻,因为我知道如何做101种方法,但不知道书中定义的方法。(注意,我懂C++)到目前为止,我们只介绍了C++的基础知识。所以基本上,我们知道变量、赋值和基本转换。在书中我遇到了这部分问题:提示用户输入十进制数将该数字转换为最近的整数并将其打印到屏幕上所以我有简单的代码:doublen;cout>n;cout(n)但我意识到这对我不起作用。它总是会截断小数点,以便1.9->1而不是预期的1.9->2如何仅使用我“知道”的内容来解决此问题?(如,没有round()或if语句等)这是标准合规问题吗?在学校我以为我在WindowsXP32位上使用VisualC++2005有类

c++ - 是否有 "safe"static_cast 替代方案?

C++11/14中的static_cast或实现此功能的库是否有“安全”替代方案?我所说的“安全”是指强制转换应该只允许不丢失精度的强制转换。因此,从int64_t到int32_t的转换只有在数字适合int32_t时才被允许,否则会报告错误。 最佳答案 有gsl::narrownarrow//narrow(x)isstatic_cast(x)ifstatic_cast(x)==xoritthrowsnarrowing_error 关于c++-是否有"safe"static_cast替代方

c++ - 避免 dynamic_cast 的模式

我有一个类:classA{public:virtualvoidfunc(){…}virtualvoidfunc2(){…}};还有一些派生类,比如B、C、D...在95%的情况下,我想遍历所有对象并调用func或func2(),因此我将它们放在一个vector,例如:std::vector>myVec;…for(autoit=myVec.begin();it!=myVec.end();++it)(*it).func();但是,在其余5%的情况下,我想根据它们的子类对类做一些不同的事情。我的意思是完全不同的,比如调用带有其他参数的函数,或者根本不调用某些子类的函数。我已经想到了一些解决这