Enum class C++11 by reference or value

c++, c++11, enum-class, move-semantics

Solution

It is not inheriting the primitive type but rather it tells the implementation to use the specified type(`unsigned short`) as the underlying type for the enumerators.

You can just simply treat the enum class object as any other class object and apply the same rules while passing it to functions.

- If you want to modify the enum class object inside function, pass it by reference.

- If you just want to read the object inside function pass it by constant reference.

Move semantics are a language run-time performance enhancing feature which makes use of opportunities to move from rvalues instead of applying copy semantics which are performance intensive. r-value references and move semantics are not only limited to move constructor and move assignment operator but they can also be used with other functions. If you have scenarios which can make use of this optimization it is perfectly fine to make use of them.

Problem

I have basically two questions may be they are related so I'll put them into one. Should we pass enum class in C++11 by reference or value when passing to function. It is sort of inheriting primitive type but is it the whole object that is passed? in since enum classes are type safe; ``` enum class MyEnumClass : unsigned short { Flag1 = 0, Flag2 = 1, Flag3 = 2, Flag4 = 4, }; ``` Now lets say we have function sig ``` const char* findVal(const MyEnumClass& enumClass); ^ should this be by const ref? __| ``` my other question is here - ``` SHOULD IT BE BY MOVE like (MyEnumClass&&) - I am still learning/understanding move semantics and rvalue so I am not sure if move semantics are only for constructors or can be for member or static funcs - ```

Original source

Related problems