初始化 std::unique_ptr 作为原始数组指针初始化

Initialize std::unique_ptr as a raw array pointer is initialized

我目前有一个小型 OpenGL 项目,其中我在堆上创建了一个数组,

float* vertices = new float[48] {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
};

我期待将其扩展到数百或数千个值。因此,我想使用 std::unique_ptr。我查看了文档,似乎无法像原始数组指针那样初始化 std::unique_ptr 。尽管如此,我还是试过写这样的东西,

std::unique_ptr<float[]> vertices(new float[48])  {
    0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,     // front top right,    0
    0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,    // front bottom right, 1
   -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,    // front bottom left,  2
   -0.5f, 0.5f, 0.5f, 0.4f, 0.8f, 0.2f,     // front top left,     3

    0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // back top right,     4
    0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,   // back bottom right,  5
   -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,   // back bottom left,   6
   -0.5f, 0.5f, -0.5f, 0.4f, 0.8f, 0.2f     // back top left,      7 
};

无法编译。

是否可以像使用原始指针数组一样初始化 unique_ptr 数组?

我考虑过使用 std::vector 然后 "capturing" 和 unique_ptr,虽然我还没有尝试过。

除非非常具体的原因,否则您应该以使用std::vector为目标。这是动态分配数组的标准类型。 my_vector.data() returns 指向数据的指针,因此它与 OpenGL 等 C api 兼容。

// on the heap
std::vector<float> vertices = {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
};

如果你想要矢量(如果你需要调整大小等),只需使用 std::vector<float> 作为类型而不是 std::array<float, 48>


现在,如果你想修复你的语法但仍然使用动态内存和指针(我建议不要这样做,因为它是针对 OpenGL 顶点的,而且你提前知道大小)只需替换类型,用里面的新数组:

std::unique_ptr<float[]> vertices(new float[48] {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
});

std::unique_ptr 类型有一个构造函数,该构造函数接受指向类型 T 的指针。构造函数只是显式的,因此只能使用直接初始化。

这是一个包含所有构造函数类型的 live example