Using C++ fixed-width integers with OpenGL to reduce header includes

I often find that I need to include the header file for my OpenGL loading library in many of the header files within my own C++ project. In a lot of cases, this is just so I can access a particular GL type such as GLuint in order to declare a class member that can store the ID of an OpenGL object.

To avoid continually including this in every header file, would it be appropriate to store an OpenGL ID within an std::uint32_t instead? To my knowledge, it is guranteed to have the same range and the same number of bytes as a GLuint.

I was also wondering whether or not I could use C++ fixed-width integers to talk with OpenGL directly, or whether they should be cast to the appropriate OpenGL type before they are passed into any of the gl functions.

If nothing here that I’ve mentioned is a viable option, then is there another alternative to this problem that I haven’t considered?

All basic OpenGL types have fixed sizes specified by the standard (table 2.2 in the 4.6 specification) with the exception of GLintptr, GLsizeiptr and GLsync, which are “large enough to store any CPU address” (i.e. the size of a pointer). Note that GLsizei is 32 bits regardless of architecture (so it’s not equivalent to C’s size_t).

I’d suggest not. A cast will prevent the compiler from warning you if you make a mistake. Also, apart from the 64-bit and platform-dependent types, you could just use the standard types (signed/unsigned char/short/int). You’re unlikely to encounter a working system where those aren’t 8/16/32 bits, and even less likely to encounter such a system which supports OpenGL. Be sure to get the signedness correct to avoid warnings.

It would probably be better to find a way to split the OpenGL type declarations out of the loading library and into its own header that has minimal dependencies and is very small. If the header is generated, perhaps you can extend its generation process to make that easier. If it’s something like GLEW, you can always just copy-and-paste the types.