-
Notifications
You must be signed in to change notification settings - Fork 443
iox-#2414 Use placement new to construct a new object during assignment in 'variant' #2413
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,25 +149,21 @@ template <typename T> | |
inline typename std::enable_if<!std::is_same<T, variant<Types...>&>::value, variant<Types...>>::type& | ||
variant<Types...>::operator=(T&& rhs) noexcept | ||
{ | ||
if (m_type_index == INVALID_VARIANT_INDEX) | ||
if (m_type_index != internal::get_index_of_type<0, T, Types...>::index) | ||
{ | ||
call_element_destructor(); | ||
// NOLINTJUSTIFICATION clang-tidy suggests that std::move(rhs) might erroneously move an lvalue, but here rhs is already T&&, so it's safe. | ||
// NOLINTNEXTLINE(bugprone-move-forwarding-reference) | ||
new (&m_storage) T(std::move(rhs)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yuanxingyang Does the suggestion from clang-tidy to use std::forward not work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It does not work; the following error occurs during compilation:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yuanxingyang move cannot be used in this context. This operator is used for copy assignment and it would be a bug if a complex type would be assigned to a default constructed variant. Unfortunately, our tests currently do not cover this scenario yet. |
||
m_type_index = internal::get_index_of_type<0, T, Types...>::index; | ||
} | ||
|
||
if (!has_bad_variant_element_access<T>()) | ||
else | ||
{ | ||
// AXIVION Next Construct AutosarC++19_03-M5.2.8: conversion to typed pointer is intentional, it is correctly aligned and points to sufficient memory for a T by design | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
auto storage = reinterpret_cast<T*>(&m_storage); | ||
*storage = std::forward<T>(rhs); | ||
} | ||
else | ||
{ | ||
error_message(__PRETTY_FUNCTION__, | ||
"wrong variant type assignment, another type is already " | ||
"set in variant"); | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.