• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // A Scalar that asserts for uninitialized access.
3 template<typename T>
4 class SafeScalar {
5  public:
SafeScalar()6   SafeScalar() : initialized_(false) {}
SafeScalar(const SafeScalar & other)7   SafeScalar(const SafeScalar& other) {
8     *this = other;
9   }
10   SafeScalar& operator=(const SafeScalar& other) {
11     val_ = T(other);
12     initialized_ = true;
13     return *this;
14   }
15 
SafeScalar(T val)16   SafeScalar(T val) : val_(val), initialized_(true) {}
17   SafeScalar& operator=(T val) {
18     val_ = val;
19     initialized_ = true;
20   }
21 
T()22   operator T() const {
23     VERIFY(initialized_ && "Uninitialized access.");
24     return val_;
25   }
26 
27  private:
28   T val_;
29   bool initialized_;
30 };
31