[英]Strange behavior in boost::variant's handling of move-only types
鉴于以下内容:
#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <boost/variant.hpp>
using boost::variant;
#endif
考虑一下这个片段。 这在c ++ 17的std::variant<>
和boost::variant<>
下编译。
struct B
{
B() = default;
B(const B&) = delete;
B(B&&) {}
B& operator=(const B&&) = delete;
B& operator=(B&&) {}
};
int main()
{
variant<B, int> v;
v = B{};
}
但是,这个其他示例仅使用C ++ 17的std::variant<>
编译,因为boost::variant<>
尝试执行复制赋值。
struct A
{
A(int) {}
};
struct B
{
B(int) {}
B(const B&) = delete;
B(B&&) {}
B& operator=(const B&) = delete;
B& operator=(B&&) {}
};
int main()
{
variant<A, B> v{A{42}};
v = B{42}; // This line doesn't compile with Boost
}
的两个例子之间的唯一显着的差异是存在struct A
和默认构造与服用构造int
。 我还发现,如果第二种情况下class B
的移动构造函数和赋值运算符是= default
ed,则可以使用Boost进行编译。 我做错了什么,或者这是Boost.Variant的问题? 尝试使用Boost 1.65和GCC 7.2.0的两个示例。
问题是move-constructor不是noexcept,这使得它不合适:
B(B&&) noexcept {};
你也可以写:
B(B&&) = default;
在这种情况下,编译器隐式生成noexcept
移动构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.