提交 978666c8 编写于 作者: A Alexander Alekhin

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

......@@ -281,13 +281,14 @@ namespace util
template<class T> typename detail::are_different<variant<Ts...>, T, variant<Ts...>&>
::type variant<Ts...>::operator=(T&& t) noexcept
{
using decayed_t = typename std::decay<T>::type;
// FIXME: No version with implicit type conversion available!
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)
{
util::get<T>(*this) = std::move(t);
util::get<decayed_t>(*this) = std::move(t);
return *this;
}
else return (*this = variant(std::move(t)));
......
......@@ -115,6 +115,18 @@ TEST(Variant, Assign_Basic)
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)
{
TestVar vis(42);
......@@ -139,6 +151,19 @@ TEST(Variant, Assign_ValueUpdate_DiffType)
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)
{
TestVar va(42);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册