gpt4 book ai didi

c++ - 为什么这个 boost::variant::operator= 调用不能编译?

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:22 24 4
gpt4 key购买 nike

为什么 v = 42 不能在这里编译?似乎编译器试图调用 Foo 的复制赋值运算符,为什么?我怎样才能让它编译?

#include <boost/variant.hpp>

struct Foo {
Foo(Foo&&) { }
};

int main() {
boost::variant<int, Foo> v;
v = 42;
}

wandbox

错误信息是:

In file included from prog.cc:1:
In file included from /usr/local/boost-1.62.0/include/boost/variant.hpp:17:
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:619:21: error: object of type 'Foo' cannot be assigned because its copy assignment operator is implicitly deleted
lhs_content = ::boost::detail::variant::move(*static_cast<T* >(rhs_storage_));
^
/usr/local/boost-1.62.0/include/boost/variant/detail/visitation_impl.hpp:112:20: note: in instantiation of function template specialization 'boost::detail::variant::move_storage::internal_visit<Foo>' requested here
return visitor.internal_visit(
^
/usr/local/boost-1.62.0/include/boost/variant/detail/visitation_impl.hpp:154:13: note: in instantiation of function template specialization 'boost::detail::variant::visitation_impl_invoke_impl<boost::detail::variant::move_storage, void *, Foo>' requested here
return (visitation_impl_invoke_impl)(
^
/usr/local/boost-1.62.0/include/boost/variant/detail/visitation_impl.hpp:240:11: note: in instantiation of function template specialization 'boost::detail::variant::visitation_impl_invoke<boost::detail::variant::move_storage, void *, Foo, boost::variant<int, Foo>::has_fallback_type_>' requested here
, BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE
^
/usr/local/boost-1.62.0/include/boost/preprocessor/repetition/repeat.hpp:29:26: note: expanded from macro 'BOOST_PP_REPEAT'
# define BOOST_PP_REPEAT BOOST_PP_CAT(BOOST_PP_REPEAT_, BOOST_PP_AUTO_REC(BOOST_PP_REPEAT_P, 4))
^
/usr/local/boost-1.62.0/include/boost/preprocessor/cat.hpp:22:32: note: expanded from macro 'BOOST_PP_CAT'
# define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
^
/usr/local/boost-1.62.0/include/boost/preprocessor/cat.hpp:29:34: note: expanded from macro 'BOOST_PP_CAT_I'
# define BOOST_PP_CAT_I(a, b) a ## b
^
<scratch space>:128:1: note: expanded from here
BOOST_PP_REPEAT_1
^
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:2384:33: note: in instantiation of function template specialization 'boost::detail::variant::visitation_impl<mpl_::int_<0>, boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2>, int, boost::mpl::l_item<mpl_::long_<1>, Foo, boost::mpl::l_end> > >, boost::mpl::l_iter<boost::mpl::l_end> >, boost::detail::variant::move_storage, void *, boost::variant<int, Foo>::has_fallback_type_>' requested here
return detail::variant::visitation_impl(
^
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:2398:16: note: in instantiation of function template specialization 'boost::variant<int, Foo>::internal_apply_visitor_impl<boost::detail::variant::move_storage, void *>' requested here
return internal_apply_visitor_impl(
^
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:2125:19: note: in instantiation of function template specialization 'boost::variant<int, Foo>::internal_apply_visitor<boost::detail::variant::move_storage>' requested here
this->internal_apply_visitor(visitor);
^
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:2171:13: note: in instantiation of member function 'boost::variant<int, Foo>::variant_assign' requested here
variant_assign( detail::variant::move(temp) );
^
/usr/local/boost-1.62.0/include/boost/variant/variant.hpp:2189:9: note: in instantiation of function template specialization 'boost::variant<int, Foo>::move_assign<int>' requested here
move_assign( detail::variant::move(rhs) );
^
prog.cc:9:7: note: in instantiation of function template specialization 'boost::variant<int, Foo>::operator=<int>' requested here
v = 42;
^
prog.cc:4:5: note: copy assignment operator is implicitly deleted because 'Foo' has a user-declared move constructor
Foo(Foo&&) {}
^

最佳答案

It seems the compiler is trying to call the copy assignment operator of Foo, why?

这只是令人困惑的措辞。移动赋值运算符(您都没有)就足够了。

至于为什么需要移动赋值运算符:boost::variantoperator=是根据另一个 boost::variant 的分配来实现的.从你的int , 一个 boost::variant<int, Foo>被构造。 v然后移动分配 boost::variant<int, Foo> .这需要考虑它被移动分配的可能性 Foo ,即使那不可能发生。

你可以看到 variant_assign在编译器的错误消息和 variant.hpp 中对此负责的辅助方法:

template <typename T>
void move_assign(T&& rhs)
{
// If direct T-to-T move assignment is not possible...
detail::variant::direct_mover<T> direct_move(rhs);
if (this->apply_visitor(direct_move) == false)
{
// ...then convert rhs to variant and assign:
//
// While potentially inefficient, the following construction of a
// variant allows T as any type convertible to one of the bounded
// types without excessive code redundancy.
//
variant temp( detail::variant::move(rhs) );
variant_assign( detail::variant::move(temp) );
}
}

虽然那里有一个允许跳过临时变体的优化,但只有当变体已经包含 int 时才有可能。 ,无论如何,它不能在编译时确定,所以它不会阻止 variant_assign 的实例化。 .

关于c++ - 为什么这个 boost::variant::operator= 调用不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com