[SDL] Const correctness for SDL video

Ryan C. Gordon icculus at icculus.org
Wed Feb 14 15:15:31 PST 2007


> Just curious, why do you care enough about this to bother with doing all
> this work? I teach C++ and I really don't see the point? Could you
> educate me on this subject?

It allows better error checking...if the implementation promises const 
safety and then modifies a field, the compiler can catch it.

It also promotes good interface design. A programmer can tell 
immediately what they can pass to a function without fear of it being 
modified between calls...the blit() thing in the patch is a good 
example...the developer knows that it can reuse srcrect in a loop, but 
dstrect will need to be reset between calls.

It also allows optimization...the compiler can make decisions about what 
is safe to leave in registers, what needs to be copied before use, etc 
by what it knows won't be changed in a function.

(and a few more rules for C++: you can't construct a temporary object in 
a function call unless the function wants a const reference:

      void myfunc(const blah &x);

      myfunc(blah(1, 2));  // only works if there's a "const" up there.

).


Making things "const correct" is always worth it in my opinion...the 
problem is that in legacy code it can be really really hard to retrofit it.

--ryan.



More information about the SDL mailing list