Search code examples
c++constantsoverhead

Overhead of const modifier for built-in types in C++


I prefer to add const modifier to all built-in arguments in functions I write. E.g.:

void foo(const int arg1, const double arg2);

is better for me than:

void foo(int arg1, double arg2);

After code review I was told that const modifier brings an overhead when it is applied for integer and built-in types. Is that true and why?

Thanks,


Solution

  • It has no more overhead than a typedef does. Your coworker is wrong.

    If you want to convince him, print out the disassembly of both variants, and show your coworker that they're the same.

    However, adding the const qualifier to primitive types like this is utterly pointless and futile. They're copied anyway, and there's no harm in modifying them. There's nothing to be gained by making them const.