Merge pull request #16849 from anton-potapov:ap/variant__assignment_operator_compile_error

pull/16899/head
Alexander Alekhin 5 years ago
commit 978666c816
  1. 5
      modules/gapi/include/opencv2/gapi/util/variant.hpp
  2. 25
      modules/gapi/test/util/variant_tests.cpp

@ -281,13 +281,14 @@ namespace util
template<class T> typename detail::are_different<variant<Ts...>, T, variant<Ts...>&> template<class T> typename detail::are_different<variant<Ts...>, T, variant<Ts...>&>
::type variant<Ts...>::operator=(T&& t) noexcept ::type variant<Ts...>::operator=(T&& t) noexcept
{ {
using decayed_t = typename std::decay<T>::type;
// FIXME: No version with implicit type conversion available! // FIXME: No version with implicit type conversion available!
static const constexpr std::size_t t_index = static const constexpr std::size_t t_index =
util::type_list_index<T, Ts...>::value; util::type_list_index<decayed_t, Ts...>::value;
if (t_index == m_index) if (t_index == m_index)
{ {
util::get<T>(*this) = std::move(t); util::get<decayed_t>(*this) = std::move(t);
return *this; return *this;
} }
else return (*this = variant(std::move(t))); else return (*this = variant(std::move(t)));

@ -115,6 +115,18 @@ TEST(Variant, Assign_Basic)
EXPECT_EQ(42, util::get<int>(vis)); EXPECT_EQ(42, util::get<int>(vis));
} }
TEST(Variant, Assign_LValueRef)
{
TestVar vis;
EXPECT_EQ(0u, vis.index());
EXPECT_EQ(0, util::get<int>(vis));
int val = 42;
vis = val;
EXPECT_EQ(0u, vis.index());
EXPECT_EQ(42, util::get<int>(vis));
}
TEST(Variant, Assign_ValueUpdate_SameType) TEST(Variant, Assign_ValueUpdate_SameType)
{ {
TestVar vis(42); TestVar vis(42);
@ -139,6 +151,19 @@ TEST(Variant, Assign_ValueUpdate_DiffType)
EXPECT_EQ("42", util::get<std::string>(vis)); EXPECT_EQ("42", util::get<std::string>(vis));
} }
TEST(Variant, Assign_LValueRef_DiffType)
{
TestVar vis(42);
EXPECT_EQ(0u, vis.index());
EXPECT_EQ(42, util::get<int>(vis));
std::string s("42");
vis = s;
EXPECT_EQ(1u, vis.index());
EXPECT_EQ("42", util::get<std::string>(vis));
}
TEST(Variant, Assign_ValueUpdate_Const) TEST(Variant, Assign_ValueUpdate_Const)
{ {
TestVar va(42); TestVar va(42);

Loading…
Cancel
Save