If I have a std::vector<Foo> and want to store its size in a GLuint, my compiler gives a warning that there is possible loss of data.

GLuint size = vec.size(); // "conversion from 'size_t' to 'GLuint', possible loss of data"

How do I correctly do this type of casting? Are there more pitfalls of this kind with for example float to GLfloat?


Practically speaking, probably not - try printing the size of each type using sizeof. Probably GLuint is 32 bits and size_t is 64 bits - so 32 bits are thrown out; but float and GLfloat are probably both 32 bit IEEE 754 numbers. "Probably" sprinkled above, because technically speaking the sizes of C ints, floats and the float format are compiler dependent, but GL type sizes (and formats) should be standard (see https://www.opengl.org/wiki/OpenGL_Type). Chances are, GLfloat is just typedef'd as a float.

You may be able to turn off this warning in your compiler - I choose to explicitly cast ("GLuint size = GLuint(vec.size())" works) so I am reminded of what's happening.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.