title: Casting a char array to an object pointer - is this UB?
body: <p>I recently saw a class like this that was used to construct objects "on-demand" without having to use dynamic memory allocation for various reasons.</p>
  
  <pre><code>#include &lt;cassert&gt;
  
  template&lt;typename T&gt;
  class StaticObject
  {
  public:
      StaticObject() : constructed_(false)
      {
      }
  
      ~StaticObject()
      {
          if (constructed_)
              ((T*)object_)-&gt;~T();
      }
  
      void construct()
      {
          assert(!constructed_);
  
          new ((T*)object_) T;
          constructed_ = true;
      }
  
      T&amp; operator*()
      {
          assert(constructed_);
  
          return *((T*)object_);
      }
  
      const T&amp; operator*() const
      {
          assert(constructed_);
  
          return *((T*)object_);
      }
  
  private:
      bool constructed_;
      alignas(alignof(T)) char object_[sizeof(T)];
  };
  </code></pre>
  
  <p>Is this code, namely the casting of a properly aligned char array to an object pointer, considered undefined behavior by the C++14 standard or is it completely fine?</p>