Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining destructor in a class derived from move-only type gives compile-time error when created with emplace_back or push_back of std::vector

Removing the comment from following code gives a compile time error. It seems that defining the destructor in derived class is causing the copy constructor to be called in emplace_back

#include <vector>

struct A
{
    A() = default;
    A( A& ) = delete;
    A& operator=( A& ) = delete;
    A( A&& ) = default;
    A& operator=( A&& ) = default;
    ~A(){};
};

struct B : public A
{
    using A::A;
    //~B() = default; //ERROR
};

int main()
{
    std::vector< B > list;
    for( int ii = 0; ii < 3; ii++ ) { list.emplace_back(); }
    return 0;
}

The error is:

In file included from /usr/include/c++/5/vector:62:0,
                 from a.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = B; _Args = {B}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; _Tp = B]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = B*; _ForwardIterator = B*; _Allocator = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:422:8:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:101:23:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
a.cpp:24:57:   required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: invalid initialization of non-const reference of type ‘B&’ from an rvalue of type ‘B’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
a.cpp:15:8: note:   initializing argument 1 of ‘B::B(B&)’
 struct B : public A
        ^

I am using the base class like A for managing HANDLEs, and want to define destructor in derived class mostly for debugging purposes. For now I am using smart pointers in vectors to avoid this issue.

I am wondering what is causing this and how to fix this by either changes in code or using more suitable container. Any help is appreciated.

Edit: I am compiling with g++ -std=c++11 g++ version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

like image 695
Aneel Avatar asked Oct 09 '19 22:10

Aneel


1 Answers

Verify your expectations of CopyConstructible and MoveConstructible with static_asserts:

static_assert(!std::is_copy_constructible<A>{});
static_assert( std::is_move_constructible<A>{});

static_assert(!std::is_copy_constructible<B>{});
static_assert(!std::is_move_constructible<B>{});

When ~B() is declared, the compiler implicitly deletes B(B&&). You can override that behavior with an explicit declaration:

B(B&&) = default;
like image 122
Howard Hinnant Avatar answered Sep 28 '22 11:09

Howard Hinnant