我正在尝试注册一个函数,该函数返回一个int以在程序结束时使用atexit()函数调用。(特别是ncurses中的endwin()函数。)但是由于atexit()需要一个指向void函数的指针,我遇到了一个问题。我尝试了以下方法:static_cast(endwin)但似乎不允许从int函数到void函数的static_cast。我想要实现的目标是否可行,如果可以,如何实现?注意:我愿意忽略函数的返回值。编辑:我还尝试创建一个lambda函数,这似乎可以满足我的要求:atexit([]{endwin();});与包装/转发功能相比,这是一个好的解决方案吗?(除了它需要C++11并且避免
这个问题在这里已经有了答案:C++11:SFINAEintemplateparameters,GCCvsClang[duplicate](1个回答)关闭6年前。考虑以下代码:#include#includestructTest{Test&operator++();};structNoIncrement{};templateusingvoid_t=void;template>structhas_pre_increment_member:std::false_type{};templatestructhas_pre_increment_member())>>:publicstd::true
这个问题在这里已经有了答案:C++11:SFINAEintemplateparameters,GCCvsClang[duplicate](1个回答)关闭6年前。考虑以下代码:#include#includestructTest{Test&operator++();};structNoIncrement{};templateusingvoid_t=void;template>structhas_pre_increment_member:std::false_type{};templatestructhas_pre_increment_member())>>:publicstd::true
请看下面的代码:unsignedchar*p=newunsignedchar[x];CLASS*t=new(p)CLASS;assert((void*)t==(void*)p);我可以假设(void*)t==(void*)p吗? 最佳答案 是的,你可以。我相信有几条规定可以保证这一点。[expr.new]/10-强调我的Anew-expressionpassestheamountofspacerequestedtotheallocationfunctionasthefirstargumentoftypestd::size_t.Tha
请看下面的代码:unsignedchar*p=newunsignedchar[x];CLASS*t=new(p)CLASS;assert((void*)t==(void*)p);我可以假设(void*)t==(void*)p吗? 最佳答案 是的,你可以。我相信有几条规定可以保证这一点。[expr.new]/10-强调我的Anew-expressionpassestheamountofspacerequestedtotheallocationfunctionasthefirstargumentoftypestd::size_t.Tha
这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher
这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher
考虑以下代码:structfoo{staticconstexprconstvoid*ptr=reinterpret_cast(0x1);};automain()->int{return0;}上面的例子在g++v4.9(LiveDemo)中编译良好,而在clangv3.4(LiveDemo)中编译失败并产生以下错误:error:constexprvariable'ptr'mustbeinitializedbyaconstantexpression问题:根据标准,这两个编译器哪个是正确的?声明此类表达式的正确方法是什么? 最佳答案 TL
考虑以下代码:structfoo{staticconstexprconstvoid*ptr=reinterpret_cast(0x1);};automain()->int{return0;}上面的例子在g++v4.9(LiveDemo)中编译良好,而在clangv3.4(LiveDemo)中编译失败并产生以下错误:error:constexprvariable'ptr'mustbeinitializedbyaconstantexpression问题:根据标准,这两个编译器哪个是正确的?声明此类表达式的正确方法是什么? 最佳答案 TL
所以,我发现了一个类似的问题here,但答案更多的是关于风格以及你是否能够做到。我的问题是,当您调用一个返回对象的非void函数但您从未分配或使用所述返回对象时,实际会发生什么?所以,少说你是否可以,因为我绝对知道你可以并且理解上面链接的另一个问题......编译器/运行时环境是做什么的?这不是特定于语言的问题,但如果您回答,请说明您所指的语言,因为行为会有所不同。 最佳答案 我相信对于C#和Java,结果最终都会在堆栈上,然后编译器会强制弹出指令忽略它。EricLippert在"Thevoidisinvariant"上的博客文章有