草庐IT

Implicit_cast

全部标签

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++ - 警告 : overflow in implicit constant conversion

在下面的程序中,第5行确实按预期给出了溢出警告,但令人惊讶的是,第4行在GCC中没有给出任何警告:http://www.ideone.com/U0BXnintmain(){inti=256;charc1=i;//line4charc2=256;//line5return0;}我在想这两行都应该给出overflow警告。还是我缺少什么?我做这个实验的主题是:typedeftypechecking?在那里我说了以下内容(我从答案中删除了,因为当我运行它时,它并没有像我预期的那样显示)://However,you'llgetwarningforthiscase:typedefintT1;ty

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