21

I've seen some methods like this:

void SomeClass::someMethod() const;

What does this const declaration do, and how can it help optimize a program?

Edit

I see that the first part of this question has been asked before... BUT, it still doesn't answer the second part: how would this optimize the program?

Unknown
  • 45,913
  • 27
  • 138
  • 182
  • 2
    Why do you think it would "optimize" the program? That is not its purpose. –  May 01 '09 at 14:09
  • Well I always heard about how declaring const for a variable would help optimization. I don't see much point in declaring const in this manner if not to help optimize. – Unknown May 01 '09 at 19:48
  • 2
    This has been [asked before.](http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration) – Ron Romero May 01 '09 at 03:48

6 Answers6

23

If the compiler knows that the fields of a class instance are not modified across a const member function call, it doesn't have to reload any fields that it may have kept in registers before the const function call.

This is sort of referred to the in C++ FAQ in the discussion on const_cast.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
  • 1
    +1 for addressing the optimization part with an explanation that makes sense. Are there any documents detailing this? – Unknown May 01 '09 at 06:56
  • Also, I have a feeling that since field access is static, shouldn't the compiler already know if it needs to reload a field in a register or not? – Unknown May 01 '09 at 06:57
  • I added a link. What do you mean about "field access is static"? – Jim Buck May 01 '09 at 14:06
  • If other threads change class state ? If this may happen, compiler will wrongly cache data from previous methods. – socketpair May 05 '22 at 03:01
  • @socketpair - yup, if doing multi-threaded programming using such a class instance, all bets are off! – Jim Buck May 06 '22 at 20:31
5

It tells the compiler that the method has no effect on the classes state; you can't assign to anything in it. Have a look at the C++ FAQ Lite 18.10.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
4

The asm code that is generated for the const method will be the same if the const is there or not. const is a function of the compiler not the runtime, so if there are any performance gains I would think that the compilers optimizer might use the const as a hint for things like inlining or determining side effects for a possible optimization. So in short the optimizer might be able to help out a bit, but if the method is straight forward to begin with then I doubt that the code generated from the optimizer would be any different const or no const.

Here's an easy optimization I use (rather than hit and miss things like const) which take a second but pay off. Organize your class variables so that they fall on cache line boundaries a little better, and put your most accessed variables together. To do it just put your ints, doubles, floats, etc. together at the top of your class variable declarations and your odd sized variables at the bottom like so:

int foo; 
int bar;
double baz;
SomeObject obj;
char ch[14];
Aaron Cook
  • 1,304
  • 9
  • 9
  • +1 for addressing optimization, though its a little bit vague. I had the same feelings that the code generated would be very similar. – Unknown May 01 '09 at 06:58
2

It allows you to call the class member function on const objects:

class SomeClass
{
public:
    void foo();
    void bar() const;
}

SomeClass a;
const SomeClass b;

a.foo();  // ok
a.bar();  // ok
b.foo();  // ERROR -- foo() is not const
b.bar();  // ok -- bar() is const

There's also the volatile qualifier for use with volatile objects, and you can also make functions const volatile for use on const volatile objects, but those two are exceedingly rare.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
0

It prevents someMethod from altering any member variable of an object of that class.

Myforwik
  • 3,438
  • 5
  • 35
  • 42
0

My first thought regarding optimization is that since the 'const' indicates that the instance's state hasn't changed, the compiler possibly has more freedom with respect to reordering nearby calls to methods on that instance.

imaginaryboy
  • 5,979
  • 1
  • 32
  • 27
  • The compiler will not do this. const stops you from modifying the object but the function can modify other things, including the target of pointers in the object. – Nick Whaley May 01 '09 at 04:54
  • Thanks. I knew that the 'const' function could obviously change mutable internal state, but I wasn't sure if that necessarily ruled out being able to make this level of optimization since from the callers perspective the object is conceptually unchanged. – imaginaryboy May 01 '09 at 14:59
  • Nick actually means that pointers inside of an object can't be changed but what they point to *can* be changed even inside of a const member function. The following is possible: class { public: int *x; void blah() const { *x = 42; // but "x = 0xblahblah;" would be illegal here } }; – Jim Buck May 01 '09 at 21:44
  • Yes, I understood what Nick was saying. Thanks. – imaginaryboy May 01 '09 at 22:52