草庐IT

my_numeric_cast

全部标签

c++ - 使用 boost::numeric_cast<>

当我想在不同的整数类型之间进行转换时,似乎最好的语法是使用boost::numeric_cast():inty=99999;shortx=boost::numeric_cast(y);//willthrowanexceptionifyistoolarge我从来没有用过;但是语法非常简单,所以一切都很好。现在假设我想做一些更高级的事情:我希望它返回目标类型的最小值或最大值(饱和度),而不是抛出异常。我想不出一种表达方式,但是documentation表明这是可能的(可能使用RawConverter策略)。我能想到的只是以下丑陋的:shortx=numeric_cast(max(min(y

c++ - static_cast 和 Implicit_cast 有什么区别?

什么是implicit_cast?我什么时候应该更喜欢implicit_cast而不是static_cast? 最佳答案 我正在复制我对answerthiscomment的评论在另一个地方。Youcandown-castwithstatic_cast.Notsowithimplicit_cast.static_castbasicallyallowsyoutodoanyimplicitconversion,andinadditionthereverseofanyimplicitconversion(uptosomelimits.you

c++ - argc 和 argv 的 void cast

我在看一段C++代码,main函数的第一行引起了我的注意:intmain(intargc,constchar*argv[]){(void)argc;(void)argv;...}除此行之外,根本不使用argc和argv。为什么作者要进行空投?是否可以阻止编译器提示未使用的变量? 最佳答案 “是否可以阻止编译器提示未使用的变量?”是的 关于c++-argc和argv的voidcast,我们在StackOverflow上找到一个类似的问题: https://sta

c++ - 在 C++ 中使用 boost::lexical_cast 将 double 转换为字符串?

我想使用lexical_cast将float转换为字符串。通常它工作正常,但我对没有小数的数字有一些问题。如何修复字符串中显示的小数位数?例子:doublen=5;stringnumber;number=boost::lexical_cast(n);结果编号是5,我需要编号5.00。 最佳答案 来自boostlexical_cast的文档:Formoreinvolvedconversions,suchaswhereprecisionorformattingneedtightercontrolthanisofferedbythedef

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++ - cin.ignore(numeric_limits<streamsize>::max(), '\n'); max() 无法识别

我正在介绍C++,我在Win7上使用VStudio2013。我尽量避免从我的菜单中输入错误的数据,它在除此之外的所有菜单中都有效。cout>move2;if(move2size){cout唯一的区别是在move>的条件中是一个变量(大小)而不是一个数字。当我输入一个字符时,它会回到要求另一个输入的问题,但是如果我输入一个单词,它就会中断!我尝试使用cin.ignore(numeric_limits::max(),'\n');但编译器会突出显示max()它说“期望标识符”。对于你们所有优秀的程序员来说,这可能很容易,但我不知道如何解决它。有人可以帮帮我吗? 最