简体   繁体   中英

How to pass a templated vector to a C++ function?

I am writting a function that needs to handle any type of vector passed into it, regardless of the inner type of the vector.

The function takes in the vector, finds the size of the elements and then passes the data to the GPU.

It would look something like:

void Object_3D::set_instance_data(Renderer* handler, vector<T> data)
{
    glBindVertexArray(VAO);

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, (VBOs[3]));
    glBufferData(GL_SHADER_STORAGE_BUFFER, data.size()*sizeof(T), 
        data.data(), GL_DYNAMIC_COPY);

}

However I cannot get this code to compile.

Looks like you need to turn this into a template member function, on this general order:

template <class T>
void Object_3D::set_instance_data(Renderer* handler, vector<T> const &data)
{
    glBindVertexArray(VAO);

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, (VBOs[3]));
    glBufferData(GL_SHADER_STORAGE_BUFFER, data.size()*sizeof(T), 
        data.data(), GL_DYNAMIC_COPY);
}

Of course, you'll have to declare it as a template member function, not just define it without declaration. Aside: although there are other ways to make this work, it's often easiest to include the definition (not just the declaration) in the header when using a template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM