Just use boost::optional<T>
instead of pair of your members m_bInitialized
and m_value
. Probably you could just use boost::optional<T>
instead of your template class Lazy
...
If you really want to make it in your own way - then steal some implementation details from boost::optional<T>
.
One hint is that this boost class uses placement new:
class Lazy {
public:
bool is_init() const { return m_memberPtr != nullptr; }
T& force()
{
if (!is_init())
m_memberPtr = new (m_memberMemory) T(m_initializer());
return *m_memberPtr;
}
private:
T* m_memberPtr;
alignas(T) char m_memberMemory[sizeof(T)]; // s
};