Question

I get an undescriptive error: expected unqualified-id before '[' token. Naturally, because this is not Java or C#, I have no clue what's going on.

Here's my code:

constexpr GLfloat[NUM_VERTS * ELEM_PER_NORM] GetNormals(bool normalize)
{
    glm::vec3 normal = glm::vec3(1.f, 1.f, 1.f);

    if (normalize)
        normal = glm::normalize(normal);

    return {-normal.x, -normal.y, -normal.z, // Back
            -normal.x, normal.y, -normal.z,
            normal.x, normal.y, -normal.z,
            normal.x, -normal.y, -normal.z,

            -normal.x, -normal.y, normal.z, // Front
            -normal.x, normal.y, normal.z,
            normal.x, normal.y, normal.z,
            normal.x, -normal.y, normal.z};
}

I also get two other errors: "expected '}' at end of input" and "expected unqualified-id at end of input". Those two send me to my source .cpp instead of my header source where my previous code is. It sends my to a function without mistakes ( void DrawBuffers::InitVboIbo(){} ).

Thank you! :)

EDIT:

Here's my new code:

constexpr std::array<GLfloat, NUM_VERTS * ELEM_PER_NORM> GetNormals(bool normalize)
{
    glm::vec3 normal = glm::vec3(1.f, 1.f, 1.f);

    if (normalize)
        normal = glm::normalize(normal);

    std::array<GLfloat, NUM_VERTS * ELEM_PER_NORM> res =
          {{-normal.x, -normal.y, -normal.z, // Back
            -normal.x, normal.y, -normal.z,
            normal.x, normal.y, -normal.z,
            normal.x, -normal.y, -normal.z,

            -normal.x, -normal.y, normal.z, // Front
            -normal.x, normal.y, normal.z,
            normal.x, normal.y, normal.z,
            normal.x, -normal.y, normal.z}};

    return res;
}

const static GLfloat normals[NUM_VERTS * ELEM_PER_NORM] = GetNormals(true);
Was it helpful?

Solution

In order to return an array from a constexpr function you should use std::array like std::array<GLfloat, NUM_VERTS * ELEM_PER_NORM>. You cannot return std::vector because it has non-constexpr constructor, you cannot return pointers to a local arrays because they get destroyed, you cannot create static const array because it must differ based on function parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top