C++ Const and Pass-By-Value
Introduction
Sometimes we would encounter library interfaces that take const
inputs by value. It turns out that such interface reflects lack of thinking in library design and implementation.
In this blog post, I would like to briefly discuss the best practice for the situation that has both const
and pass-by-value.
Const and Pass-By-Value Interface and Implementation
Suppose we are going to create a shared library libfoo.so
that contains a bar
function which takes in one int
value as input and returns an int
value as output. We could have the following two interface options.
1 | int bar(int v); |
1 | int bar(int const v); |
Since pass-by-value makes a copy of the variable, when the user reads the the interfaces, the user knows that both interface will not alter the original value passed to the function. So the question now becomes which interface we should choose in terms of designing the library. Because the interface should not assume too much about how the function will be implemented, the best interface should be the first one.
With the first interface, we could come up with two implementations that does the same thing, one changes the value of v
during runtime and one does not.
1 |
|
1 |
|
The first implementation is slightly more efficient because the it does not have an additional local variable compared to the second one, but the second implementation is also valid.
If the interface is the second one, the implementation can only be the second one, and in some situations, such as the one that I mentioned above, the interface prevents us from creating more efficient implementations.
Example
Taken together, we picked the first interface and the second implementation as the library and created an additional main executable as a full example and compile.
1 | int bar(int v); |
1 |
|
1 |
|
1 | $ g++ -c -o foo.o foo.cpp -std=c++11 |
Conclusions
The best practice for const
and pass-by-value is not having const
in the interface and adding const
if necessary in the implementation.
C++ Const and Pass-By-Value