C++ - Is it better to pass a enum class as value or const reference?
Solution 1
enum class holds an integral value just like a regular enum so you can safely pass it by value without any overhead. Notice that compiler may sometimes optimize pass by reference as well by replacing it with pass by value. But passing by reference may result in some overhead when such an optimization is not applied.
Solution 2
As a very broad rule of thumb, pass plain old data types as values, and class instances as const references.
There are exceptions to this rule; and indeed the trendies nowadays like to rely on pass by value followed by move semantics when building copy constructors for example.
For the new enum class stuff of C++11, pass it by value (after all it only holds an integral type under the hood) and trust the compiler to make the optimisations.
If you are ever in any doubt, profile any performance differences.
Solution 3
Passing by reference is effectively passing by pointer. Just because that specific reference to the item is constant, doesn't mean it cannot be changed. It just can't be changed through that reference. The memory location may be changed by other references.
So there is a genuine semantic difference between a const reference and passing the value itself.
If this semantic does not come into play for what you are writing, and you are trying to pass by const reference instead of by value for the purposes of avoiding a copy, this does not apply to things like integers or enums (regardless of the "class" label on your enum). You're really just making things slower by going through a pointer.
Solution 4
"Plain" enums and enum class objects both are of integral type, so the decision of passing by const reference or by value is just the same as if you did for other arguments of integral type.
If the one or the other it is "better" depends on what you want to achieve, and holding a reference to an object or getting copy of it is not only a subject of space or "performance". Note that storing a (const) reference to an object reflects changes that are made to this object at any other place, while storing a copy of the object does not.
So: decide mainly from the perspective "does passing by reference is semantically correct". If you get in troubles with memory/performance, use your profiler and measure before unintendedly changing semantics.
Anyway, from a memory or performance perspective, I think that references usually introduce some (probably very small) overhead, since accessing the actual value means an additional indirection.
From the memory perspective, int's are often 4 bytes when "pointers" (or references) are represented by 8 bytes. But I think that this should not weigh to much in a function argument, unless you have many such arguments in a recursive function with very high recursion depth.
Additionally, enum classes can define their underlying data type, e.g. as an unsigned char, though I still do not think that this makes to much difference when used as function argument.
enum class ColorsEnumClass : unsigned char {
red,
green,
blue
};
AutobahnPolizei
Updated on June 15, 2022Comments
-
AutobahnPolizei 6 monthsSince its called a 'class' I would usually pass it as const reference, but if I use a plain enum it makes no difference right? So does it make a difference if I pass a enum class as value/const ref? Also, does the type matter? For example a enum class:int
-
HostileFork says dont trust SE over 5 yearsI imagine the reason you are downvoted here is for the unsolicited explanation of the feature difference between an enum class and an enum, which the OP did not ask about. They're clearly asking about the performance characteristic, and so using the phrase "pretty significant difference" runs counter to the very premise of the question... since it's an integer at runtime, with some nice compile-time features. -
Marek Vitek over 5 yearsI don't see it as performance only question. Wording "but if I use a plain enum it makes no difference" also implies that he looks for difference between enum and enum class as such. I know why I was downvoted. It was mistake I made when putting together first version of my post. Points are not everything :-). -
HostileFork says dont trust SE over 5 yearsI think your premise is incorrect there. Who would use an enum class without knowing what it gave you at compile time? That's the documented, obvious part. The less obvious part is the implication he's asking about, specifically whether the conventional wisdom of passing classes by const reference should apply. -
Marek Vitek over 5 years@HostileFork You might be right. I have seen people asking for such obvious things many times. They just don't know where to look. And someone told them use this instead of that. But only OP knows for sure what he meant. And we will probably never know given his record of marking correct answer.