c++performancepass-by-valueenum-classpass-by-const-reference

C++ - Is it better to pass an enum class as value or const reference?


Since it's 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 an enum class as value or as const ref? Also, does the type matter? For example an enum class:int.


Solution

  • 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.