The Controlled Vars library is a set of templates that help you avoid bugs in your C++ software by using safe guards for all the basic C++ types.

Quite often you create a class with a set of members. Write the first version of the contructor and everything works perfectly. Later, you come back to your class and decide that you need another variable member. You add that int and ... forget to initialize it in your constructor. The compiler doesn't tell you anything since this is perfectly legal C++ code. Some compiler may be capable of telling you that you read the integer before it is defined, but frankly, if you count on that, you will not find the problem very often.

Instead, you can use controlled variables. The library offers 3 methods:

  1. Automatically initialized to a default value (internally we offer a version that sets your variables to zero.) The drawback with this method is that your member is always initialize even if you end up never using it.
  2. Force you to intialize the variable, which means that if you forget to put it in your constructor(s), the compiler will tell you on your next compile run. The drawback with this method is that you have to initialize this variable even if you end up never using it.
  3. Leave the value to whatever random number it is set to, but generate an error if read before being initialized. This is similar to what C++ does, plus the advantage of getting an error if you attempt to read the value before you initalized it. The one drawback with this method is that the variable is not of the right size (sizeof(member) != sizeof(basic_type).)

Note that all of the side effects disappear once you remove the Controlled Vars debug flag. For example, the random number version becomes a very simple typedef of the corresponding type.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.