mardi 21 juin 2016

Why can't non-temporary std containers be passed by non-const reference, neither by address? [on hold]

Here's a summary of the code:

struct texel
{
    GLfloat r;
    GLfloat g;
    GLfloat b;
    GLfloat a;  // alpha
};

static void GenerateThreeBeamTexture(std::vector<texel> *putDataHere,
    const unsigned int maxTexelRows, const unsigned int texelsPerRow)
{
    // stuff that fills out putDataHere
}

GLuint CreateTexture()
{
    // stuff

    const unsigned int MAX_TEXEL_ROWS = 64;
    const unsigned int TEXELS_PER_ROW = 64;
    std::vector<texel> crude2DTextureIn1D;
    GenerateThreeBeamTexture(&crude2DTextureIn1D, MAX_TEXEL_ROWS, TEXELS_PER_ROW);

    // more stuff

    // give back a handle to what was created
    return textureId;
}

I hope this isn't just my compiler. I'm using VS2015 (platform toolset v140) and I'm getting an "argument of type A is incompatible with parameter of type B" error.

I have to fill out an array, so I want to use a container that will slap me in the face my index math is wrong instead of silently swallowing it, so I opted for a std::vector<...>, and since I am filling it out, I want to pass it by non-const address. If I spell out std::vector as a variable type in CreateTexture() and as a parameter type for GenerateThreeBeamTexture(...), then I get an error at the "&" character in the call to GenerateThreeBeamTexture(...):

argument of type "std::vector<texel, std::allocator<texel>> *" is incompatible with parameter of type "std::vector<texel, std::allocator<texel>> *"

...I want to slap the compiler. Those are the exact same types.

But if I encapsulate the std::vector<...> in another data structure or put it into a typedef (not shown), the compiler is happy.

Given the trivial difference between putting the type in a typedef and spelling it out, I am now concerned that I am running into a corner case of the compiler that wasn't covered because it was deemed unsafe. Is my code a memory bomb waiting to happen?

Aucun commentaire:

Enregistrer un commentaire