相关疑难解决方法(0)

在C++ 11和Boost.Container下,vector :: resize(size_type n)的这种行为是否正确?

我有一个C++ 03应用程序,其中std::vector<T>类型作为临时缓冲区使用.因此,它们通常会使用std::vector<T>::resize()以确保它们足够大以在使用前保存所需数据.这个函数的C++ 03原型实际上是:

void resize(size_type n, value_type val = value_type());
Run Code Online (Sandbox Code Playgroud)

因此,实际上在调用时resize(),通过添加适当数量的副本来放大矢量val.但是,通常我只需要知道它vector足够大以容纳我需要的数据; 我不需要用任何值初始化它.复制构造新值只是浪费时间.

C++ 11拯救了(我想):在它的规范中,它分为resize()两个重载:

void resize(size_type n); // value initialization
void resize(size_type n, const value_type &val); // initialization via copy
Run Code Online (Sandbox Code Playgroud)

这非常适合C++的理念:只需支付你想要的东西.正如我所指出的那样,我的应用程序不能使用C++ 11,所以当我遇到Boost.Container库时,我很高兴,它表明在其文档中支持这个功能.具体来说,boost::container::vector<T>实际上有三个重载resize():

void resize(size_type n); // value initialization
void resize(size_type n, default_init_t); // default initialization
void resize(size_type n, const value_type &val); // initialization via copy
Run Code Online (Sandbox Code Playgroud)

为了验证我理解了所有内容,我进行了快速测试以验证C++ 11的行为std::vector<T>boost::container::vector<T> …

c++ boost vector c++11

23
推荐指数
2
解决办法
7457
查看次数

allocator construct()默认初始化而不是值初始化吗?

作为这个问题的后续,默认的allocator(std::allocator<T>)需要construct按如下方式实现(根据[default.allocator]):

template <class U, class... Args>
void construct(U* p, Args&&... args);
Run Code Online (Sandbox Code Playgroud)

效果:::new((void *)p) U(std::forward<Args>(args)...)

也就是说,始终是值初始化.结果是std::vector<POD> v(num),对于任何pod类型,将值初始化num元素 - 这比默认初始化num元素更昂贵.

为什么没有 std::allocator提供默认初始化的额外过载?就是这样的(借用凯西):

template <class U>
void construct(U* p) noexcept(std::is_nothrow_default_constructible<U>::value)
{
    ::new(static_cast<void*>(p)) U;
}
Run Code Online (Sandbox Code Playgroud)

是否有理由在呼叫案例中更喜欢值初始化?对我来说这似乎令人惊讶,这打破了通常的C++规则,我们只支付我们想要使用的内容.


我认为这样的改变是不可能的,因为目前std::vector<int> v(100)会给你100 0秒,但我想知道为什么会这样......因为人们可以很容易地std::vector<int> v2(100, 0)以同样的方式要求有差异之间new int[100]new int[100]{}.

c++ vector allocator language-lawyer c++11

14
推荐指数
1
解决办法
537
查看次数

标签 统计

c++ ×2

c++11 ×2

vector ×2

allocator ×1

boost ×1

language-lawyer ×1