草庐IT

枚举法

全部标签

c++ - 用 Cython 包装枚举类

我正在尝试将枚举类包装在C++头文件中,以便在cython项目中使用。例如,这怎么可能enumclassColor{red,green=20,blue};用Cython包裹。 最佳答案 CPP类enumclassColor{red,green=20,blue};类型定义cdefexternfrom"colors.h":cdefcppclassColor:pass颜色类型的定义cdefexternfrom"colors.h"namespace"Color":cdefColorredcdefColorgreencdefColorblue

c++ - 为什么我应该更喜欢类中的 static constexpr int 而不是类级整数常量的枚举?

C++17更新:staticconstexpr变量是隐式内联,因此不需要外部定义。原始问题:假设我有一个常量列表,例如structCls{staticconstexprintN=32;staticconstexprintM=64;};这当然建议我为这些添加定义以避免可能发生的ODR使用问题,因此我需要:constexprintCls::N;constexprintCls::M;为什么应该我更喜欢这个structCls{enum:int{N=32,M=64};};这让我免于ODR使用的麻烦,因为N和M更真实地只是常量,而不是对象本身(如果这是标题,那就更重要了-only)并且更短。我可以

c++ - 为什么不能转发声明范围枚举?

标题已经是问题了。更多细节:标准制定:Iftheenum-keyisfollowedbyanested-name-specifier,theenum-specifiershallrefertoanenumerationthatwaspreviouslydeclareddirectlyintheclassornamespacetowhichthenested-name-specifierrefers(i.e.,neitherinheritednorintroducedbyausing-declaration),andtheenum-specifiershallappearinanames

c++ - 强枚举 typedef : clang bug or c++11 standard uncertainty?

对于这样的代码:typedefenumFooEnum:intFooEnum;enumFooEnum:int{A=1,B};clang(linux/7.0.0)报告没有错误[-c-std=c++11-pedantic],但是gcc(linux/8.2.1)不编译它:g++-c-std=c++11-pedantictest2.cpptest2.cpp:1:28:error:expected';'or'{'before'FooEnum'typedefenumFooEnum:intFooEnum;^~~~~~~test2.cpp:1:28:error:expectedclass-keybefo

c++ - 在创建 C++ 枚举和依赖数据结构时如何避免重复自己?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Enumtostring:returntheenumintegervalueifinvalid/notfound简而言之,我的(工作)定义代码是这样的:enumGadget{First,Second,};constchar*gadget_debug_names[]={"First","Second",//note:stringsaresameasenumtokenshere,butbonuspointsif//theycanoptionallybegivendifferentvalues};但是,如果信息位于

c++枚举并使用它们从用户输入

我的代码中有一个枚举,如下所示:enumStatus{In-Active,Active};。状态对象作为参数传递给Person对象,因此我希望用户输入Active或In-Active并将它们保存在Status变量中。我怎样才能做到这一点?我一开始尝试过,但没有用。这是我的代码:#includeusingnamespacestd;enumStatus{InActive,Active};classPerson{private:stringname;intage;Statusstatus;public:Person(conststring&_name,constint_age,constSt

c++ - 我应该如何检查基础类型值是否为枚举值?

设I是某种整型。现在假设我有一个enumclassmy_enum_class:I,其值可能不连续。现在我得到了一些I值。如何检查它是否是my_enum_class中枚举的值?对asimilarquestion的回答(对于C语言)假设值是连续的,并且可以添加一个“虚拟”上限值,并检查0和该值之间的范围;这与我的情况无关。还有其他方法吗? 最佳答案 目前没有办法做到这一点。有反射(reflection)提案可能会进入c++20和/或c++23让你迭代(在编译时,因此在运行时)枚举中的枚举值。使用它检查会相对容易。有时人们会进行手动枚举反

c++ - 如何枚举所有可用的网络接口(interface)?

这个问题在这里已经有了答案:HowtoenumeratenetworkadaptersandgettheirMACaddressesinWin32APIC++?(1个回答)关闭5年前。如何枚举计算机上当前可用的所有网络接口(interface)(包括虚拟、未连接、环回等)?我需要知道他们的IP4/6、掩码、网关、DNS、WINS等语言:C++、WinAPI系统:Windows2000及更高版本(包括Win7)

c++ - 枚举转发声明

这个问题在这里已经有了答案:ForwarddeclaringanenuminC++(19个回答)关闭9年前。我正在尝试正确使用枚举的前向声明。因此,我搜索了Internet,但找不到有用的东西。我在标题中使用它://ForwarddeclarationenummyEnumProcessState;然后我在结构中使用这个枚举:structmyStruct{[...]myEnumProcessStateosState;[...]};在另一个标题中:enummyEnumProcessState{eNotRunning,eRunning};我发现应该将类型放在要接受的枚举前向声明中。但是,我不

c++ - 如果枚举标签与类型不匹配,编译器是否应该发出警告?

我希望编译器给出这样的警告:“香蕉不是一种颜色。”我知道在switch语句的上下文中,标签被提升为int,编译器对0很满意并且不关心它是“Green”还是“Banana”。我希望GCC的-Wconversion可以解决问题。enumColor{Green=0};enumFruit{Banana=0};intmain(){Colorc=Green;switch(c){caseBanana:std::cerr 最佳答案 强类型枚举:C++11引入了强类型enums,使用enumclass:#includeenumclassColor{G