问题
Deleting this question in favor the following; an answer to which now handles classes with no default constructor:
How to abstract lazy initialization in C++?
In a nutshell, the code uses placement new/delete. See http://en.wikipedia.org/wiki/Placement_syntax for details...
回答1:
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
};
来源:https://stackoverflow.com/questions/17632944/how-can-i-create-a-lazy-c-template-class-that-handles-types-with-no-default-co