Skip to content

[Aws::Crt::Optional] avoid std::aligned_storage #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions include/aws/crt/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ namespace Aws
Optional() : m_value(nullptr) {}
Optional(const T &val)
{
new (&m_storage) T(val);
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(val);
m_value = reinterpret_cast<T *>(m_storage);
}

Optional(T &&val)
{
new (&m_storage) T(std::forward<T>(val));
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(std::forward<T>(val));
m_value = reinterpret_cast<T *>(m_storage);
}

~Optional()
Expand All @@ -45,8 +45,8 @@ namespace Aws
return *this;
}

new (&m_storage) T(std::forward<U>(u));
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(std::forward<U>(u));
m_value = reinterpret_cast<T *>(m_storage);

return *this;
}
Expand All @@ -55,8 +55,8 @@ namespace Aws
{
if (other.m_value)
{
new (&m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(m_storage);
}
else
{
Expand All @@ -68,8 +68,8 @@ namespace Aws
{
if (other.m_value)
{
new (&m_storage) T(std::forward<T>(*other.m_value));
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(std::forward<T>(*other.m_value));
m_value = reinterpret_cast<T *>(m_storage);
}
else
{
Expand Down Expand Up @@ -101,8 +101,8 @@ namespace Aws

if (other.m_value)
{
new (&m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(m_storage);
}

return *this;
Expand Down Expand Up @@ -132,8 +132,8 @@ namespace Aws

if (other.m_value)
{
new (&m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(m_storage);
}

return *this;
Expand Down Expand Up @@ -163,8 +163,8 @@ namespace Aws

if (other.m_value)
{
new (&m_storage) T(std::forward<U>(*other.m_value));
m_value = reinterpret_cast<T *>(&m_storage);
new (m_storage) T(std::forward<U>(*other.m_value));
m_value = reinterpret_cast<T *>(m_storage);
}

return *this;
Expand Down Expand Up @@ -196,7 +196,7 @@ namespace Aws
}

private:
typename std::aligned_storage<sizeof(T)>::type m_storage;
alignas(T) char m_storage[sizeof(T)];
T *m_value;
};
} // namespace Crt
Expand Down