Assume the following class:
class Example
{
public:
...
Example& operator=(const Example& rhs);
...
private:
other_type *m_content;
size_t m_content_size;
}
Example& Example::operator=(const Example& rhs)
{
if (this != &rhs)
{
delete m_content;
m_content = nullptr;
m_content = getCopiedContent(rhs);
}
return *this;
}
I know that this is not the best way to implement operator=
but that's on purpose, because my question is about these two lines:
m_content = nullptr;
m_content = getCopiedContent(rhs);
Can be that the compiler will optimize out m_content = nullptr;
even though getCopiedContent
is not defined as throw()
or noexcept
:
other_type* getCopiedContent(const Example& obj);
On one hand the compiler may assume that if right after m_content = nullptr;
I overwrite the value of m_content
with the return value of getCopiedContent
, it may optimize out the whole m_content = nullptr;
expression. On the other hand if the compiler optimizes it out and getCopiedContent
throws an exception, m_content
will contain a non valid value.
Does C++ standard state anything regarding such scenario?