草庐IT

lv_switch

全部标签

c++ - 在 switch 语句中从 int 到 enum 类的隐式转换

enumclasspid{Alpha,Beta,Gamma};intmain(){intpropId=2;switch(propId){casepid::Alpha:casepid::Beta:casepid::Gamma:break;}}以上片段在msvc2012中编译良好(并且有效)但在clang-3.4和g++-4.8中失败。这些需要static_cast(propId)在switch子句中使用。顺便说一下,没有显式转换的简单赋值,例如pida=propId;在每个编译器中给出错误。谁做对了? 最佳答案 标准第4条,“标准转换

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - 哪些提升类型用于 switch-case 表达式比较?

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?#include"stdio.h"constcharOPTION=(char)(unsignedchar)253;intmain(intargc,char*argv[]){unsignedcharc=253;switch(c){caseOPTION:printf("option\n");break;default:printf("unknown\n");break;}return0;}在查看C++标准(N36902013-05-05)时,我看到了switch的子句:6.4.2Theswitchstatement2Th

c++ - 我可以在 C++ switch 语句中匹配范围而不是单个值吗?

我是编程新手。是否可以使用,>在开关盒中?例如,.........inti;cin>>i;......switch(i){case20 最佳答案 C++不提供用于匹配范围的switch语法。当范围相对较小时,您可以提供case标签,并依赖fall-through:switch(i){case20:case21:case22:case23:case24:case25:doSomething();break;case26:case27:case28:case29:doSomethingElse();break;...}对于中等大小的范​

c++ - "constexpr if"比 switch 语句好吗?

C++17引入了根据编译时条件实例化的“constexprif”。这是否意味着在模板函数中使用“constexprif”比使用switch语句更好?例如:templatevoidfunc(){ifconstexpr(val==0){}elseifconstexpr(val==1){}else...ifconstexpr(val==k){}else{}}//vstemplatevoidfunc(){switch(val){case0:break;case1:break;...casek:break;default:break;}} 最佳答案

php - 在 switch 语句中重复代码

我有这样的switch语句:switch(x){casea:executeSth();executeA();break;caseb:executeSth();executeB();break;...}所以executeSth();除了在默认情况下应该总是执行,但在它之后调用一些特定情况的代码(executeA();或executeB()等等)。(所以简单地把它放在开关前面是行不通的)。有没有一种有效的方法来减少“executeSth();”的数量?不牺牲性能?我只能想象将它分成两个开关(一个执行executeSth()和一个执行特定代码),但这会牺牲性能。也许您有更好的想法?我基本上对

c++ - 使用 switch 语句的函数没有返回

我正在使用较旧的gcc版本(如果我没记错的话是7.something)在LINUX中开发一个应用程序。最近我试图在Windows上运行相同的应用程序。在Windows上,我使用MinGW作为编译器(使用gcc8.1.0)。我在Windows上编译我的应用程序时遇到了这个错误消息:warning:controlreachesendofnon-voidfunction[-Wreturn-type]代码类似如下:classmyClass{protected:enumclassmyEnum{a,b,};intfun(myClass::myEnume);}和intmyClass::fun(myC

c++ - switch 语句和对象隐式 int 转换

在C++中,直接在隐式转换为int的对象上使用switch语句是否合法/正确?而不是使用返回对象标记的方法。classAction{public:enumEType{action1,action2,action3};operatorint()const{returnmType;}private:ETypemType;/*...*/}intmain(){Actiona=/*...*/switch(a){caseAction::EType::action1:/*...*/break;caseAction::EType::action2:/*...*/}} 最佳答

c++ - MSVC : what compiler switches affect the size of structs?

我有两个单独编译的DLL,一个是从VisualStudio2008编译的,一个是从matlab编译的mex文件。两个DLL都包含一个头文件。当我在一个DLL中采用sizeof()结构时,它返回48,而在另一个DLL中它返回64。我检查了/Zp开关,在两个编译中它都设置为/Zp8。还有哪些其他编译器开关可能会影响结构的大小?该结构是一个简单的POCO,没有继承,也没有虚函数。编辑结构看起来像这样:classLIBSPECSGeometry{public:std::vectorm_i;uintN;uintn_im,n_s;};在调试中,sizeof()在两种情况下都返回56,在发行版中,在