static_assert 失败,因为 std::vector 的值类型是可破坏的
static_assert failed because value type is destructible for std::vector
我有一个非常简单的程序。不确定为什么 static_assert(is_destructible<_Value_type>::value
失败。
<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,
实现如下:
#include <iostream>
#include <vector>
using namespace std;
class MovieData {
MovieData(){}
~MovieData(){}
};
typedef vector<MovieData> Movies;
int main()
{
Movies result; // Line 16
return 0;
}
如果析构函数被注释 // ~MovieData(){}
程序编译。有人能解释一下为什么我的析构函数会导致问题吗?
问题是您的 class 析构函数是 private
(默认情况下,所有 声明的 class 成员也是如此)。添加 public:
行,你应该没有问题:
class MovieData {
public:
MovieData(){}
~MovieData(){}
};
static_assert failed because value type is destructible for std::vector
不,断言失败,因为值类型不可可破坏。
Can some once explain why destructor is causing an issue ?
如果声明私有析构函数,则 class 不可破坏(在 class 的成员函数之外)。如果要将 class 的实例存储在向量中,请不要声明私有析构函数。
If destructor is commented // ~MovieData(){} the program compiles.
这是修复程序的好方法。
我有一个非常简单的程序。不确定为什么 static_assert(is_destructible<_Value_type>::value
失败。
<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,
实现如下:
#include <iostream>
#include <vector>
using namespace std;
class MovieData {
MovieData(){}
~MovieData(){}
};
typedef vector<MovieData> Movies;
int main()
{
Movies result; // Line 16
return 0;
}
如果析构函数被注释 // ~MovieData(){}
程序编译。有人能解释一下为什么我的析构函数会导致问题吗?
问题是您的 class 析构函数是 private
(默认情况下,所有 声明的 class 成员也是如此)。添加 public:
行,你应该没有问题:
class MovieData {
public:
MovieData(){}
~MovieData(){}
};
static_assert failed because value type is destructible for std::vector
不,断言失败,因为值类型不可可破坏。
Can some once explain why destructor is causing an issue ?
如果声明私有析构函数,则 class 不可破坏(在 class 的成员函数之外)。如果要将 class 的实例存储在向量中,请不要声明私有析构函数。
If destructor is commented // ~MovieData(){} the program compiles.
这是修复程序的好方法。