In a practical environment, using gcc or MS Visual Studio, is it bad to pass the value types which are the same size or less than an int by const reference ?
i.e. is it bad to write such a function:
void f(const bool& b);
or
void f(const char& c);
rather than:
void f(bool b);
or
void f(char c);
The reason I am asking is that I do not see the benefit of passing a reference in these cases but maybe I am missing something.
By using '&' you can pass it by reference.
We can pass an argument by const reference, also referred to as a reference to const. It can be more efficient to pass an argument by reference, but to ensure it is not changed, we make it of const reference type.
2) For passing large sized arguments: If an argument is large, passing by reference (or pointer) is more efficient because only an address is really passed, not the entire object.
C does not support references or passing by reference. You should use pointers instead and pass by address. Pass-by-value is efficient for primitive types, but does a shallow copy for structs. In C++ it makes a LOT of sense to pass objects by reference for efficiency.
It may be slightly bad, or it may not have an effect at all (depends on where the original value is stored, how good the optimizer is, and how it decides to treat your code).
The standard doesn't mandate how references are to be implemented, but in practice compilers implement references using pointers. Therefore in the general case a bool&
would be implemented using a bool*
, which means that to access the bool
you need an extra pointer dereference each time. Since a bool
is no bigger than a pointer, there's no reduced memory footprint or less byte copying to offset this drawback.
As a result the accepted practice is to pass primitives around as values since it's more efficient. Of course although passing such around as references won't really blow up anything, and unless you are accessing the value inside a loop will probably not even result in any measurable difference.
Performance aside, there are actually cases where you will get different behavior.
For instance, passing a const
reference makes sure that the function cannot change the value of the referenced variable, but another thread might do so. If you pass by reference (even with const
), you will see these changes, if you pass by value, you will not.
Also, the definition of the interface limits what you can do with the variable inside the function. Consider this example:
int foo(int a) {
a = 5; // valid
}
int bar(const int& a) {
a = 5; // compiler-error
}
If you pass by reference, and you want to modify the value of the variable for local use, you need to make an extra copy. If you pass by value, you already have a copy.
One reason would be that you would like to convey to other programmers that the value is constant, it may in some cases be clearer although const bool
would suffice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With