For instance, if I have this code:
class SomeDataProcessor
{
public:
bool calc(const SomeData & d1, const SomeData & d2) const;
private:
//Some n
Yes, modern C compilers can elide redundant function calls if and only if they can prove that such an optimization behaves as-if the original program semantics were followed. For example, that means means they could eliminate multiple calls to the same function with the same arguments, if the function has no side-effects and if its return value depends only on the arguments.
Now, you asked specifically about const
- this is mostly useful to the developer, and not the coder. A const
function is a hints that the method doesn't modify the object it is called on, and const
arguments are hints that the arguments are not modified. However, the function may (legally1) cast away the const
-ness of the this
pointer or of its arguments. So the compiler can't rely on it.
Furthermore, even if const
objects passed to a function really were never modified within that function, and const
functions never modified the receiver object, the method could easily rely on mutable global data (and could mutate such data). Consider, for example, a function that returns the current time, or which increments a global counter.
So the const
declarations help the programmer, not the compiler2.
However, the compiler might be able to use other tricks to prove the calls are redundant:
const
function attributes which inform gcc
that the functions has no side effects, and depend only on their parameters (and on global variables, in the case of pure
).1 Usually, as long as the object wasn't originally defined as const
.
2 There is at one sense in which const
definitions do help the compiler: they can put global objects defined as const
into a read-only section of the executable (if such a feature exists) and also combine such objects when they are equal (e.g., identical string constants).