This is a good question, and it would be useful to be able to easily check, but in general C ++ solutions can actually support a combination of errors, verbosity, and obfuscation, which makes them more tedious than the problem of trying to solve. However - for the sake of discussion - there are several approaches to this ...
You can create a class that does not have a default constructor:
template <typename T> class Explicitly_Initialised { public: Explicitly_Initialised(const T& t) : t_(t) { } T& operator T() { return t_; } const T& operator T() const { return t_; } private: T t_; };
Or you may have a class that issues if the object is read before it is assigned a value (perhaps only in "debug" assemblies, if you have such a concept). You can decide if the value from operator= was ok. You will need a member bool has_been_initialised_; to track this parameter, set false in the default constructor. One of the problems would be that operator T() does not know if the variable should be read before it is written, so to do the thorough work, you may need a proxy object to manage this situation: painful.
Alternatively, and too messy to actually recommend — you could hack a class that accepted a list of types, had a member object for each of these types, and insisted on matching the list in the constructor; it would be difficult to allow this generic class to accept ordinary arbitrary identifiers for accessing data members (it is easy to allow numerical access or even access to a type if the types were unique) (perhaps you could make it work with macro hackers).
source share