4

Correct my if I'm wrong, but the reason you pass integers and other basic data types by value is because the memory they occupy is too small, so it's a waste to make a pointer variable of that data type (which will probably be at least the same size as the data type).

That's why I always pass ints and other basic types by value to functions, and other (bigger) data types are passed by const references or by pointers of const. Did I grasp this right?

Now I've seen many APIs that pass enum types as const references, like so:

enum FileOptions { ReadOnly, ReadWrite, WriteOnly };
void processFile(const FileOptions &options);

As far as I know, enums are usually interpreted by the compiler as plain integers, so why are they passed by references? Is it done to abstract the data type from the developer, so he won't think of FileOptions as an integer? (although it is).

McLovin
  • 3,295
  • 7
  • 32
  • 67
  • 6
    http://stackoverflow.com/questions/6834581/c-is-it-better-to-pass-an-enum-as-a-value-or-as-a-const-reference – novice Jan 22 '16 at 09:33

4 Answers4

1

The only advantage I can see is, that if you later decide to create a class with extended funcitonality, you can pass it still efficiently without changing the code everwhere.

Any decent optimizer doesn't care for the reference if it is a const reference for a base type anyway, and creates the same code in both cases.

Another consequence could be if the reference is to some other variable and it is changed by another thread, it can even change it's value during the course of your function (even though it says it's const) which, I would think, is not really desireably. In this case it would heavily depend on the optimization what value is used in a given branch of that method.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

You are right, the enum is basically a 'const int'.

Second guessing why some developer chose a certain style is futile.

Unless you call the function in a very close loop it doesn't matter anyhow.

A.Franzen
  • 725
  • 4
  • 10
  • 1
    performance wise it won't matter anyway, usually the jumping and register saving for the function call will be more overhead, than the copying of the pointer or value. (unless the optimizer inlines the function) – Kai Iskratsch Jan 22 '16 at 09:36
0

In modern C++ there is also enum class which is strongly typed enums. I think const reference is used only for similarity and easy to modificate in future.

For example you have a class with a lot of methods, all parameters in this methods usually complex objects and of course passed by const reference. So you declare the enum parameter in the same way. And you will know for sure that it's should not be changed and so on

Later you may deside to convert enum in more complex object, and you will no need to update function declaration at all. It's already suitable for such refactoring

Jeka
  • 1,364
  • 17
  • 33
  • Since there is the opinion that the caller just doesn't understand what he's doing, could you explain what similarity you mean, and what "easy modification in the future"? – gnasher729 Jan 22 '16 at 09:37
  • You expect to replace an enum with something that is so big and complex that it should be passed as const& and not as a value? Really? – gnasher729 Jan 22 '16 at 09:49
  • @gnasher729 You will never know what you will have to do in future. It's a good practice to pass parameters as a const reference even if it just a struct with 2 fields, so you don't need smth "so big and complex". – Jeka Jan 22 '16 at 09:53
-1

There is one big difference: If you pass options as a value, then it becomes a local variable in your function, and only your function can change it. If you pass options as a const&, then there is a variable somewhere, which might be a static or global variable, and if your function modifies any other variable of type FileOptions, then it might be the one that was passed to you as a const&, so the value of options might change.

And if your function calls another function, then that function might change the variable behind options. So as a developer, after any function call you could suspect that options might have changed (although if that happens, that would be about the worst possible programming style imaginable), but also the compiler would have to assume that it has changed unless it can prove otherwise.

Modifying a const& in that way would be awful, so we can safely assume that unless the caller absolutely hates you a const& won't be changed. You use const& when you actually want to pass a value, but passing it by reference seems to be more efficient. For an int that is pointless, for an array containing ten megabytes worth of int's it's not at all pointless.

Another use for const& instead of value is for objects that cannot easily be copied. For example a mutex cannot easily be copied. Or an object giving exclusive access to a file can by definition not be copied in a meaningful way.

gnasher729
  • 51,477
  • 5
  • 75
  • 98