std :: variant并在GCC8中移动构造函数Bug

时间:2018-04-03 21:31:55

标签: c++ c++17 variant

我有这段代码在GCC7.2,clang 7和MSVC中工作得非常好,但在GCC 8.0中却没有。显然,它涉及复制构造函数......

#include <iostream>
#include <variant>
#include <memory>
#include <type_traits>

struct EmptyTree{};

struct Node {
    using node = std::unique_ptr<Node>;
    using Tree = std::variant<node, EmptyTree>;

    Node(int v) : value(v){}
    template<typename left, typename right>
    Node(int v, left &&l, right &&r) : value(v), left(std::move(l)), right(std::move(r)){}
    int value;
    Tree left = EmptyTree{};
    Tree right = EmptyTree{};
};

using Tree = Node::Tree;

auto make_node(int v) {
    return std::make_unique<Node>(v);
}

int main() {
    Tree a;
    a = Tree{make_node(5)};
    return 0;
}

用于测试代码的wandbox https://wandbox.org/permlink/Z8yXCen0fqbV3PDD

是Gcc 8的错误还是代码中的错误?

我收到了这个错误:

In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/8.0.1/variant: In instantiation of 'void std::__detail::__variant::__erased_assign(void*, void*) [with _Lhs = std::unique_ptr<Node>&; _Rhs = const std::unique_ptr<Node>&]':
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:607:27:   required from 'std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>& std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>::operator=(std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {std::unique_ptr<Node, std::default_delete<Node> >, EmptyTree}]'
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:648:12:   required from here
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:258:42: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Node; _Dp = std::default_delete<Node>]'
       __variant::__ref_cast<_Lhs>(__lhs) = __variant::__ref_cast<_Rhs>(__rhs);
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/wandbox/gcc-head/include/c++/8.0.1/memory:80,
                 from prog.cc:3:
/opt/wandbox/gcc-head/include/c++/8.0.1/bits/unique_ptr.h:395:19: note: declared here
       unique_ptr& operator=(const unique_ptr&) = delete;
                   ^~~~~~~~
In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/8.0.1/variant: In instantiation of 'void std::__detail::__variant::__erased_ctor(void*, void*) [with _Lhs = std::unique_ptr<Node>&; _Rhs = const std::unique_ptr<Node>&]':
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:468:30:   required from 'std::__detail::__variant::_Copy_ctor_base<<anonymous>, _Types>::_Copy_ctor_base(const std::__detail::__variant::_Copy_ctor_base<<anonymous>, _Types>&) [with bool <anonymous> = false; _Types = {std::unique_ptr<Node, std::default_delete<Node> >, EmptyTree}]'
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:509:7:   required from 'std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>& std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>::operator=(std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {std::unique_ptr<Node, std::default_delete<Node> >, EmptyTree}]'
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:648:12:   required from here
/opt/wandbox/gcc-head/include/c++/8.0.1/variant:246:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Node; _Dp = std::default_delete<Node>]'
       ::new (__lhs) _Type(__variant::__ref_cast<_Rhs>(__rhs));
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/wandbox/gcc-head/include/c++/8.0.1/memory:80,
                 from prog.cc:3:
/opt/wandbox/gcc-head/include/c++/8.0.1/bits/unique_ptr.h:394:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^~~~~~~~~~

0 个答案:

没有答案