Choosing the Default Value of an Enum Type Without Having to Change Values

Choosing the default value of an Enum type without having to change values

The default for an enum (in fact, any value type) is 0 -- even if that is not a valid value for that enum. It cannot be changed.

Is there a way to specify an enum-class value without having to type out its scope every time?

If you going to be typing a lot more of the nuisance scope-prefixes than there
are constants in the enum class, then it may be worth your while to go with this
way:

enum class result {
ERROR_BAD_PARAMETER = -3,
ERROR_FILE_NOT_FOUND = -2,
ERROR_GENERAL = -1,
SUCCESS = 0
};

constexpr result RESULT_ERROR_BAD_PARAMETER = result::ERROR_BAD_PARAMETER;
constexpr result RESULT_FILE_NOT_FOUND = result::ERROR_FILE_NOT_FOUND;
constexpr result RESULT_ERROR_GENERAL = result::ERROR_GENERAL;
constexpr result RESULT_SUCCESS = result::SUCCESS;

result foo() {
return RESULT_SUCCESS;
}

int main()
{
switch (foo())
{
case RESULT_SUCCESS:
;
}
// ^ warning: enumeration value ‘...’ not handled in ...

if (foo() == RESULT_SUCCESS) {
return 0;
}

/* error: no match for ‘operator==’
if (foo() == false) {
return -1;
}
*/
}

(g++ -Wall -Wextra -pedantic -std=c++11)

Default values for enums using the default keyword?

It is the value produced by (myEnum)0. For example:

enum myEnum
{
foo = 100
bar = 0,
quux = 1
}

Then default(myEnum) would be myEnum.bar or the first member of the enum with value 0 if there is more than one member with value 0.

The value is 0 if no member of the enum is assigned (explicitly or implicitly) the value 0.

How do I set a default value using enum type in column Mysql using Knex.js?

Did you try defaultTo() :

defaultTo — column.defaultTo(value)

Sets the default value for the column on an insert.

From knex Documentation

Example :

table.enu('role', ['one', 'two', 'three']).defaultTo('two', options={})

Adonis Enu/Enum documentation

Adonis defaultTo documentation

Are default enum values in C the same for all compilers?

Yes. Unless you specify otherwise in the definition of the enumeration, the initial enumerator always has the value zero and the value of each subsequent enumerator is one greater than the previous enumerator.

What is the default value for enum variable?

It is whatever member of the enumeration represents the value 0. Specifically, from the documentation:

The default value of an enum E is the value produced by the expression (E)0.

As an example, take the following enum:

enum E
{
Foo, Bar, Baz, Quux
}

Without overriding the default values, printing default(E) returns Foo since it's the first-occurring element.

However, it is not always the case that 0 of an enum is represented by the first member. For example, if you do this:

enum F
{
// Give each element a custom value
Foo = 1, Bar = 2, Baz = 3, Quux = 0
}

Printing default(F) will give you Quux, not Foo.

If none of the elements in an enum G correspond to 0:

enum G
{
Foo = 1, Bar = 2, Baz = 3, Quux = 4
}

default(G) returns literally 0, although its type remains as G (as quoted by the docs above, a cast to the given enum type).



Related Topics



Leave a reply



Submit