0

我正在做一些图形编程,我有一个二维数组(在程序执行期间大小不同),我使用 openGL 存储。
所以当我去访问它时,我得到的只是一个返回的void指针

为了使逻辑更容易,我希望编译器假装它是,并将其用作 2D 数组(因为arr[i][j]比 更简洁且不易出错ptr[i * y + j])。


我发现这种巧妙的铸造方法在 GCC 中运行良好(在 uni 的 linux 机器上):

Vertex (&vertices)[tess][tess] = *reinterpret_cast<Vertex (*)[tess][tess]>(
    glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)
);

这基本上将内存指针块 openGL 给我一个tess X tess二维数组,并创建该类型的引用来指向它。
这使我可以像访问内存一样vertices[i][j]
Vertex只是一个包含s的typedefedstructfloat

但是,在我的 Windows 机器上,VS'12 非常适合,抱怨它需要tess写入的整数constant(特别是; error C2057: expected constant expression)。
我不知道为什么。

现在,我知道VS 不支持 VLA,但我没有在这里创建一个数组,我正在创建一个我不知道“直到运行时”大小的东西的引用。
所以它不应该关心函数调用之间的大小是否发生变化,对吧?为什么不允许这样做?


不要被吓倒我尝试使用std::array

std::array<std::array<Vertex, tess>, tess>& vertices;

除了很明显,references must be initialized这个测试对我没有帮助,因为它仍然抱怨expression must have a constant value特别是;error C2975: '_Size' : invalid template argument for 'std::array', expected compile-time constant expression


我对在这里尝试什么感到茫然,我为reinterpret_cast它以及它使事情变得多么简单而感到自豪,并且确信我没有使用违反标准的方法。
我不想std::vector从指针创建一个,然后在完成后将该动态数组中的数据复制回指针位置;当内存块已经坐在那里时,这似乎效率很低!
没有办法围绕预先存在的内存块创建向量,是吗?..不,这听起来很傻。

我想看看这是否可以在不放弃的情况下完成并将其用作Vertex*;想法?
有人可以告诉我为什么它不能在 VS 中工作吗?
我可以做些什么来让它工作(VS的扩展/更新)?
VS'13 是否添加了对此的支持?

我也收到了C2087: 'vertices' : missing subscript我无法解释的错误。
除了这些似乎表明 VS 迫切希望tess保持不变的其他错误:
error C2466: cannot allocate an array of constant size 0
error C2540: non-constant expression as array bound
error C2440: 'initializing' : cannot convert from 'Vertex [1][1]' to 'Vertex (&)[][1]'

4

1 回答 1

0

那很有趣;我实现了一个类来处理我想要的。
它不像我想要的那样类型安全,但我从中学到了很多东西,
就像我在发现 jQuery 之前为 javascript 实现应该是规范的一部分、语法糖式的功能一样。

基本上,而不是能够做到这一点。

int (&array)[x][y] = *reinterpret_cast<int (*)[x][y]>(pointer);

你必须这样做

MDAI<int, 2> array = MDAI<int, 2>(pointer, x, y);

但除此之外,它完美无瑕!:D
我最初只写了一个专门的 TwoDArray 类,但发现我实际上也有一些 3D 数组。
因此,我没有实现 3D 版本(当您向下钻取时返回 TwoDArray),而是做了一些更通用的东西,可以帮助您处理任意多维的数组。


#include <Windows.h>
#include <iostream>

/*MultiDimensional Array Interpretation
has the compiler use a flat pointer reference as if it were a faceted array

C++11/GCC VLA-supporting equivalent:
int (&array)[x][y] = *reinterpret_cast<int (*)[x][y]>(pointer);

using MDAI, <C++11 and MSVS compatible:
MDAI<int, 2> array = MDAI<int, 2>(pointer, x, y);
*/
template<class Type, unsigned int dimension>
class MDAI {
private:
    Type* array;
    //+1 to guard against zero-length-array
    unsigned int bounds[dimension + 1];

public:
    //unfortunately I can't use `unsigned int &(dimensions)[dimension]` to make it safe
    //because of how operator[]() tries to construct its return value
    /*constructor*/
    MDAI(Type* array, unsigned int* bounds)
    : array(array)
    {
        std::copy(bounds, bounds + dimension, this->bounds);
    }

    /*programmer usable constructor for typing of the dimensions, instead of having to declare an array*/
    MDAI(Type* array, ...)
    : array(array)
    {
        va_list arguments;
        va_start(arguments, array);
        for (int index = 0; index < dimension; ++index)
            bounds[index] = va_arg(arguments, unsigned int);
        va_end(arguments);
    }

    /*drills down one level into the multi dimensional array*/
    MDAI<Type, dimension - 1> operator[](unsigned index) {
        if (dimension < 1) {
            std::cerr << "MDAI is not an array.\n";
            throw 1;
        }
        if (index < 0 || index >= bounds[0]) {
            std::cerr << "Index out of bounds.\n";
            throw 1;
        }

        //figure out how many addresses to jump
        for (unsigned int index2 = 1; index2 < dimension; ++index2)
            index *= bounds[index2];

        return MDAI<Type, dimension - 1>(array + index, bounds + 1);
    }

    /*'dereferences' the array to get a reference to the stored value*/
    Type& operator*() {
        if (dimension > 0) {
            std::cerr << "MDAI is an array.\n";
            throw 1;
        }

        return *array;
    }

    /*allows the compiler to automagically 'convert' the MDAI into whatever the user thinks it is*/
    operator Type&() {
        return **this;
    }

    /*makes assignment work automagically too!*/
    MDAI<Type, dimension>& MDAI<Type, dimension>::operator=(Type value) {
        **this = value;
        return *this;
    }
};

测试边界 2-4-3 的三维数组:

void main(unsigned int argC, char** argV) {
    using namespace std;

    int array[2][4][3] = {
        {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9},
            {10, 11, 12}
        },
        {
            {13, 14, 15},
            {16, 17, 18},
            {19, 20, 21},
            {22, 23, 24}
        }
    };

    //cast array to pointer, then interpret
    MDAI<int, 3> mdai((int*)array, 2, 4, 3);
    //testing correct memory access
    cout << 15 << ' ' << mdai[1][0][2] << endl;

    //testing modifcations using mdai are in array
    mdai[0][2][1] = -1;
    cout << array[0][2][1] << ' ' << mdai[0][2][1] << endl;

    //testing modifications in array show up in mdai
    array[1][3][2] = -23;
    cout << -23 << ' ' << mdai[1][3][2] << endl;

    //testing automatic type casting
    cout << -15.0 << ' ' << mdai[0][0][1] * -7.5 << endl;
}

它就像我将它作为数组引用一样无缝。

为了编译时安全,我想重新声明operator*()为,特别是;
Type& MDAI<Type, 0>::operator*()
所以你只能在 <X, 0> 上调用它,
但我想不通。
同样 getoperator[]()只出现在大于 0 的维度上
哦,运行时检查必须足够好

于 2014-09-13T13:33:47.677 回答