草庐IT

non-void

全部标签

c++ - 逗号运算符和 void 表达式

我遇到了这个代码片段1intreturn_printChar1(){//code//oops!noreturnstatement}intreturn_printChar2(){//codereturnprintf("Return");}intmain(){inti;//somemorecodei=return_printChar2();if((return_printChar1(),i)){printf("Gotcha");}}1:这不是现实生活中的例子。我的问题是“C和C++中是否明确定义了代码片段的行为?”我的看法:在C中,行为定义明确,因为6.5.17说Theleftoperan

c++ - 为什么不允许使用 0 作为 void* 的默认非类型模板参数

为什么下面的代码编译失败?尽管这样做是合法的void*ptr=0;templatevoidfunc();intmain(){func();return0;}我问是因为我发现一个非常可信的来源做了类似的事情,但在我的机器上编译失败注意应该将编译器错误与我的问题一起发布,所以在这里so_test.cpp:1:23:error:nullnon-typetemplateargumentmustbecasttotemplateparametertype'void*'template^static_cast()so_test.cpp:1:17:note:templateparameterisdec

c++ - 将_cast 重新解释为 void 是否合法*

我在看https://en.cppreference.com/w/cpp/language/reinterpret_cast我注意到它指定了我们始终可以转换为的合法类型:字节*char*unsignedchar*但是我没有在列表中看到void*。这是疏忽吗?我的用例需要reinterpret_cast,因为我正在从int**转换为void*。我最终将从void*转换回int**。 最佳答案 这些类型不受严格的别名规则约束。这并不意味着它们是您可以与reinterpret_cast一起使用的唯一类型.在将对象指针转换为另一种对象指针类

C++ 风格指南 : why to have non-lvalues on the left side?

合一C++codingstyleguide,我发现了一个特别的建议(第41页,建议编号53):Alwayshavenon-lvaluesontheleftside(0==iinsteadofi==0).我不明白这有什么用?要坚持这种做法吗?我不是,我也不知道为什么他是个好习惯。我能想到的唯一优点是,这将避免将无意分配误认为是比较(if(foo=0){}与if(foo==0){})对于我为什么要使用它,您有任何其他想法吗? 最佳答案 是的,你猜对了。这是好的,老Yodacondition!!!

c++ - 为什么它在这里使用 void** ?

代码取自v8-0.2.5/***Checkswhethertwohandlesarethesame.*Returnstrueifbothareempty,oriftheobjects*towhichtheyreferareidentical.*Thehandles'referencesarenotchecked.*/templatebooloperator==(Handlethat){void**a=reinterpret_cast(**this);void**b=reinterpret_cast(*that);if(a==0)returnb==0;if(b==0)returnfals

c++ - 是否有其他类型的 void_t 的标准概括?

在C++17中,我们有std::void_t,这让SFINAE看起来更漂亮:templatestd::void_tfoo(){/*stuff*/}只有T::prop存在,模板函数才会存在。如果T::prop存在,模板函数foo()将等同于:templatevoidfoo(){/*stuff*/}否则,代码相当于根本没有声明foo()。对于标准库中的其他类型,std::void_t是否有任何泛化,例如:templateusinggeneric_t=T;以便下面的代码有效?templatestd::generic_tfoo(){/*stuff*/}相当于templateintfoo(){/

c++ - `invalid initialization of non-const reference` 是什么意思?

编译此代码时,我得到以下error:Infunction'intmain()':Line11:error:invalidinitializationofnon-constreferenceoftype'Main&'fromatemporaryoftype'Main'这是我的代码:templatestructMain{staticMaintempFunction(){returnMain();}};intmain(){Main&mainReference=Main::tempFunction();//我不明白为什么?谁能解释一下? 最佳答案

c++ - int main(void) 在 C++ 中有效吗?

C++标准列出了main允许的形式。它没有将intmain(void)列为允许的形式。但是,它通常指出Theparameterlist(void)isequivalenttotheemptyparameterlistintmain(void)是一种允许的形式吗? 最佳答案 来自N3936标准草案:3.6Startandtermination3.6.1Mainfunction2Animplementationshallnotpredefinethemainfunction.Thisfunctionshallnotbeoverloade

c++ - void() 和 void{} 有什么区别?

基本上,我想知道为什么编译器拒绝ptr2声明:intmain(){//thisoneworksdecltype(void())*ptr1;//thisonedoesnotdecltype(void{})*ptr2;}看看thiscode如果你认为ptr1是一个函数指针:#includeusingnamespacestd;templatevoidf(Tt){cout输出是voidf(T)[withT=void*]。 最佳答案 [expr.type.conv]2TheexpressionT(),whereTisasimple-type-

c++ - 我可以在类定义中放置 "non-static blocks"代码吗?

C++中有非静态block吗?如果不是,如何优雅地模拟?我想替换像这样的东西:-classC{public:voidini(){/*somecode*/}};classD{std::vectorregis;//willini();laterpublic:Cfield1;public:Cfield2;public:Cfield3;//wheneverIaddanewfield,Ihaveto...#1public:D(){regis.push_back(&field1);regis.push_back(&field2);regis.push_back(&field3);//#1...al