enums

Why unscoped enums' declaration compiles?

安稳与你 提交于 2020-07-19 06:45:30
问题 In the Effective Modern C++ book of Scott Meyers it is mentioned that one of the main difference between unscoped and scoped enums(enum class) is that we can't forward declare the former (see Chapter 3, Item 10 - "Prefer scoped enums to unscoped enums" ). For example: enum Color; *// error!* enum class Color; *// fine* But I've written below mentioned small example and saw that it is not so. test.h #pragma once enum names; void print(names n); test.cpp #include "test.h" #include <iostream>

Cannot assign a string value to typescript enum (Initializer type string not assignable to variable type)

若如初见. 提交于 2020-07-18 11:00:28
问题 It seems that from TypeScript 2.4 onwards String Enums are a feature. However the following does not work: enum Foo { A = "A", B = "B" } var foo : Foo = "A"; Initializer type string not assignable to variable type Foo String literals work: type Foo = "A" | "B"; But what if I want to use an enum ? Is there a way around this? 回答1: You can use an index expression to get the value of the enum: enum Foo { A = "A", B = "BB" } var foo : Foo = Foo["A"]; var fooB : Foo = Foo["B"]; Note that the key

How to store swift enum in core data?

霸气de小男生 提交于 2020-07-18 05:31:08
问题 Swift allows you to define enum but core data doesn't support (out of the box) how to save them. The recommended solution I have seen on the internet (and used thus far) is to use a private variable: class ManagedObjectSubClass : NSManagedObject { enum Cards : Int { case Diamonds, Hearts } @nsmanaged var cardRaw: Int var card : Cards { set { self.cardRaw = newValue.rawValue } get { return Cards(RawValue:cardRaw)! } } } An alternate solution is given in the answer below. 回答1: Another approach

How to change the integer type used by an enum (C++)?

本秂侑毒 提交于 2020-07-15 06:29:26
问题 If I have an C++ enum: enum Foo { Bar, Baz, Bork, }; How do I tell the compiler to use a uint16_t to actually store the value of the enum? EDIT: Does GCC support this feature in its implementation of C++11? 回答1: You cannot do this in C++98/03. C++11 does allow you to do it, and without enum class the way everyone else seems to keep telling you: enum EnumType : uint16_t { Bar, Baz, Bork, }; Again, you don't have to use enum class . Not that it's a bad idea, but you don't have to. Does GCC

How to change the integer type used by an enum (C++)?

萝らか妹 提交于 2020-07-15 06:29:06
问题 If I have an C++ enum: enum Foo { Bar, Baz, Bork, }; How do I tell the compiler to use a uint16_t to actually store the value of the enum? EDIT: Does GCC support this feature in its implementation of C++11? 回答1: You cannot do this in C++98/03. C++11 does allow you to do it, and without enum class the way everyone else seems to keep telling you: enum EnumType : uint16_t { Bar, Baz, Bork, }; Again, you don't have to use enum class . Not that it's a bad idea, but you don't have to. Does GCC

Visualize enum value

痞子三分冷 提交于 2020-07-10 08:35:13
问题 How can i visualize the types of food with type enum? typedef struct cat{ int code; int age; float weight; enum {kibbles,canned_food,tuna_fish}food; } cats; int n,i; printf("Insert a number: "); scanf("%d",&n); cats *cat_arr = calloc(n, sizeof(cats)); for(i = 0;i<n;i++){ printf("Code: "); scanf("%d",&cat_arr[i].code); printf("Age: "); scanf("%d",&cat_arr[i].age); printf("weight: "); scanf("%f",&cat_arr[i].weight); printf("Food: "); scanf("%d",&cat_arr[i].food); } for(i=0;i<n;i++){ if(cat_arr

can an enum hold unsigned integers greater than INT_MAX?

▼魔方 西西 提交于 2020-07-09 09:48:11
问题 enum Some_Flag { SOME_FLAG_A = 0x00000000u, SOME_FLAG_B = 0x00000001u, SOME_FLAG_C = 0x00000002u, /* ... */ SOME_FLAG_Z = 0x80000000u, }; uint32_t a; a = SOME_FLAG_Z; Assuming 32 bit integers... Is this valid in C? The standard seems ambiguous to me. EDIT: Quoting the standard: 6.4.4.3 Enumeration constants Semantics 2 An identifier declared as an enumeration constant has type int. Forward references: enumeration specifiers (6.7.2.2). 6.7.2.2 Enumeration specifiers Constraints 2 The

Get enum name in Python without class name

北战南征 提交于 2020-07-09 04:04:13
问题 I would like to create my own enum. This will have names but no values. When calling this enum it should always return the name. from enum import Enum class myEnum(Enum): def __repr__(self): return self.name my_enum = myEnum('enum', ['a', 'b']) With: print(my_enum.a) it will returns a. That's ok. But using this in a class: class T(): def do_something(self): print(my_enum.a) With: T().do_something() will return enum.a Goal is this will always return a. 回答1: If When calling this enum it should

Dynamically create an enum with custom values in Python?

孤街醉人 提交于 2020-07-06 05:27:21
问题 I would like to create an enum type at runtime by reading the values in a YAML file. So I have this: # Fetch the values v = {'foo':42, 'bar':24} # Create the enum e = type('Enum', (), v) Is there a proper way to do it? I feel calling type is not a very neat solution. 回答1: You can create new enum type using Enum functional API: In [1]: import enum In [2]: DynamicEnum = enum.Enum('DynamicEnum', {'foo':42, 'bar':24}) In [3]: type(DynamicEnum) Out[3]: enum.EnumMeta In [4]: DynamicEnum.foo Out[4]:

Type-casting enum to integer and vice versa [closed]

百般思念 提交于 2020-07-05 08:04:06
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Improve this question I have an enum enum MYENUM { VAL_1 = 0, VAL_2, VAL_3 }; and two functions that have integer and enum as parameters respectively void MyIntegerFunction(int integerValue) { ... } void MyEnumFUnction(MYENUM enumValue) { ... } I have two variables int intVar = 10;