- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
根据 this example (左例)
#include <array>
#include <boost/container/static_vector.hpp>
struct X {
int k;
std::array<int, 4> a;
boost::container::static_vector<int, 4> b;
~X() = default;
};
int huh()
{
std::array<X, 5> x;
return 0;
}
看起来像boost::container::static_vector<T, N>
当 T
时可以轻易破坏是(当 b
被销毁时,不会在 X
上循环)。 huh
优化为 xor eax, eax; ret
(即 return 0
不遍历数组。
当我改用具有非平凡析构函数的包含类型时(右例)
#include <array>
#include <boost/container/static_vector.hpp>
struct Y {
~Y();
};
struct X {
int k;
std::array<int, 4> a;
boost::container::static_vector<Y, 4> b;
~X() = default;
};
int huh()
{
std::array<X, 5> x;
return 0;
}
有一个循环发生
add rbx, 1
call Y::~Y() [complete object destructor]
cmp rbx, r12
jne .L3
到目前为止,我认为这是有道理的。具有普通可破坏对象的 static_vector 占用的内存可以在恒定时间内释放,而不管实际存储了多少对象。
令我惊讶的是 std::is_trivially_destructible<boost::container::static_vector<int, 4> >::value
的值是假的。这只是一个不正确的类型特征吗?
最佳答案
boost::container::static_vector<X, N>
源自 boost::container::vector<X, ...>
具有一些定义的构造函数的类。即使它的所有主体在发布时都被编译器删除,该类也已经不容易被破坏:
struct X
{
};
struct Y
{
~Y() = default;
};
struct Z
{
~Z() {};
};
static_assert(std::is_trivially_destructible<X>::value, ""); // Succeeds
static_assert(std::is_trivially_destructible<Y>::value, ""); // Succeeds
static_assert(std::is_trivially_destructible<Z>::value, ""); // Fails
这就是为什么它不在当前的 boost 实现中的技术原因。可以有另一个实现 std::is_trivially_destructible<boost::container::static_vector<int, 4> >::value
是真的?是的,这是可能的,但我想它需要 boost::container::static_vector
的特化对于平凡可破坏的类型。
关于c++ - boost static_vector 而不是 std::is_trivially_destructible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55239440/
tl; dr:我认为我的 static_vector 有未定义的行为,但我找不到它。 这个问题是在 Microsoft Visual C++ 17 上。我有这个简单且未完成的 static_vecto
根据 this example (左例) #include #include struct X { int k; std::array a;
在另一个容器(如 std::vector)中使用 boost::container::static_vector 时,gcc 返回编译错误。以下测试用例重现了错误: #include #includ
我是一名优秀的程序员,十分优秀!