相关疑难解决方法(0)

默认值,值和零初始化混乱

我对value-&default-&zero-initialization非常困惑.特别是当他们参与不同的标准C++ 03C++ 11(和C++ 14)时.

我引用并试图在这里扩展一个非常好的答案Value-/Default-/Zero-初始化C++ 98C++ 03,以使其更加通用,因为它可以帮助很多用户,如果有人可以帮助填写需要差距,以便对何时发生的情况有一个很好的概述?

通过示例的全面见解简而言之:

有时新运算符返回的内存将被初始化,有时它不会取决于您正在新建的类型是POD(普通旧数据),还是它是一个包含POD成员且正在使用的类编译器生成的默认构造函数.

  • C++ 1998中,有两种类型的初始化:默认初始化
  • C++ 2003第三种类型的初始化中,添加了值初始化.
  • C++ 2011/C++ 2014中,仅添加了列表初始化,并且value-/default-/zero-initialization的规则稍有改变.

假设:

struct A { int m; };                     
struct B { ~B(); int m; };               
struct C { C() : m(){}; ~C(); int m; };  
struct D { D(){}; int m; };             
struct E { …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++03 c++98 c++14

81
推荐指数
2
解决办法
1万
查看次数

由于标准容器中元素的默认初始化导致性能下降

(是的,我知道有一个问题几乎相同的标题,但得到的答复是不能令人满意的,见下文)

编辑抱歉,原始问题没有使用编译器优化.现在已经修复了这个问题,但是为了避免琐碎的优化并且更接近我的实际用例,测试已经分成两个编译单元.

std::vector<>具有线性复杂性的构造函数在性能关键应用程序方面是一个令人讨厌的事实.考虑这个简单的代码

// compilation unit 1:
void set_v0(type*x, size_t n)
{
  for(size_t i=0; i<n; ++i)
    x[i] = simple_function(i);
}

// compilation unit 2:
std::vector<type> x(n);                     // default initialisation is wasteful
set_v0(x.data(),n);                         // over-writes initial values
Run Code Online (Sandbox Code Playgroud)

当构建时浪费了大量时间x.正如这个问题所探讨的那样,传统的方法似乎只是保留存储和使用push_back()来填充数据:

// compilation unit 1:
void set_v1(std::vector<type>&x, size_t n)
{
  x.reserve(n);
  for(size_t i=0; i<n; ++i)
    x.push_back(simple_function(i));
}

// compilation unit 2:
std::vector<type> x(); x.reserve(n);        // no initialisation
set_v1(x,n);                                // using push_back()
Run Code Online (Sandbox Code Playgroud)

然而,正如我的评论所指出的那样,push_back()本质上是缓慢的,这使得第二种方法实际上比第一种方法慢 …

c++ containers multithreading c++11

26
推荐指数
3
解决办法
1296
查看次数

避免标准容器中元素的默认构造

我有兴趣构建一个uninitialized_vector容器,它在语义上std::vector与警告相同,否则将使用无参数构造函数创建的新元素将在没有初始化的情况下创建.我主要是想避免将POD初始化为0. 据我所知,通过std::vector与一种特殊的分配器结合,无法实现这一点.

我想以同样的方式构建我的容器std::stack,它适应用户提供的容器(在我的情况下std::vector).换句话说,我想避免重新实现整体,std::vector而是围绕它提供一个"立面".

有没有一种简单的方法来控制"外部"的默认构造std::vector


这是我到达的解决方案,这启发了Kerrek的答案:

#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <cassert>

// uninitialized_allocator adapts a given base allocator
// uninitialized_allocator's behavior is equivalent to the base
// except for its no-argument construct function, which is a no-op
template<typename T, typename BaseAllocator = std::allocator<T>>
  struct uninitialized_allocator
    : BaseAllocator::template rebind<T>::other
{
  typedef typename BaseAllocator::template rebind<T>::other super_t;

  template<typename U>
    struct rebind
  {
    typedef uninitialized_allocator<U, BaseAllocator> …
Run Code Online (Sandbox Code Playgroud)

c++ stl initialization

11
推荐指数
2
解决办法
1461
查看次数

使用std :: unique_ptr的双(二维)数组

我有一个指针分配给指针的双数组.

  // pointer to pointer
  int **x = new int *[5];   // allocation
  for (i=0; i<5; i++){
      x[i] = new int[2];
  }

  for (i=0; i<5; i++){      // assignment
      for (j=0; j<2; j++){
          x[i][j] = i+j;
      }
  }

  for (i=0; i<5; i++)   // deallocation
      delete x[i];
  delete x;
Run Code Online (Sandbox Code Playgroud)

我试图这样做unique_ptr:

std::unique_ptr<std::unique_ptr<int>[]> a(new std::unique_ptr<int>[5]);
  for (i=0; i<5; i++)
      a[i] = new int[2];
Run Code Online (Sandbox Code Playgroud)

但一直都是这样说的错误no operator = matches these operands.我在这做错了什么?

c++ unique-ptr c++11

10
推荐指数
3
解决办法
2万
查看次数