Search code examples
c++c++14stdvector

static_assert failed because value type is destructible for std::vector


I have a very simple program. Not sure why static_assert(is_destructible<_Value_type>::value fails.

<source>:16:12:   required from here
/opt/compiler-explorer/gcc-8.1.0/include/c++/8.1.0/bits/stl_construct.h:133:21: error: static assertion failed: value type is destructible
       static_assert(is_destructible<_Value_type>::value,

Here is the implementation:

#include <iostream>
#include <vector>

using namespace std;

class MovieData {
    MovieData(){}
    ~MovieData(){}
};

typedef vector<MovieData> Movies;

int main()
{   
    
    Movies result; // Line 16
    return 0;
}

If destructor is commented // ~MovieData(){} the program compiles. Can some once explain why my destructor is causing an issue?


Solution

  • static_assert failed because value type is destructible for std::vector

    No, the assert fails because the value type is not destructible.

    Can some once explain why destructor is causing an issue ?

    If you declare a private destructor, then the class is not destructible (outside of the member functions of the class). Don't declare a private destructor if you want to store instances of the class in a vector.

    If destructor is commented // ~MovieData(){} the program compiles.

    This is a good way to fix the program.