Home How to pass a templated vector to a C++ function?
Reply: 1

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

Makogan
1#
Makogan Published in 2018-02-12 01:46:29Z

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.

Jerry Coffin
2#
Jerry Coffin Reply to 2018-02-12 02:25:23Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309496 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO