c++11实现c++14的optional(二)
sign(const Optional& other)
{
if (other.IsInit())
{
Copy(other.m_data);
m_hasInit = true;
}
else
{
Destroy();
}
}
void Assign(Optional&& other)
{
if (other.IsInit())
{
Move(std::move(other.m_data));
m_hasInit = true;
other.Destroy();
}
else
{
Destroy();
}
}
void Move(data_t&& val)
{
Destroy();
new (&m_data) T(std::move(*((T*)
(&val))));
}
void Copy(const data_t& val)
Destroy();
new (&m_data) T(*((T*) (&val)));
}
private:
bool m_hasInit;
data_t m_data;
};
复制代码
测试代码:
复制代码
void TestOptional()
{
Optional a("ok");
Optional b("ok");
Optional c("aa");
c = a;