Is it possible to pass an enum as a parameter to a variadic function? I’m trying to do the following:
GLenum ShaderManager::initialize()
{
GLuint program = loadShader("Flat", 2, ATTRIBUTE_VERTEX, "coord3d", ATTRIBUTE_TEXTURE0, "texcoord");
//...
}
GLuint ShaderManager::loadShader(std::string shaderName, ... )
{
GLuint program;
//...
va_list arglist;
va_start(arglist, shaderName);
int count = va_arg(arglist, int);
for(int i = 0; i < count; i++) {
AttributeLocation location = va_arg(arglist, AttributeLocation);
char * name = va_arg(arglist, char *);
glBindAttribLocation(program, location, name);
}
va_end(arglist);
//...
}
Where both ATTRIBUTE_VERTEX and ATTRIBUTE_TExTURE are declared as
enum AttributeLocation {
ATTRIBUTE_VERTEX = 0,
ATTRIBUTE_COLOR,
ATTRIBUTE_NORMAL,
ATTRIBUTE_TEXTURE0
};
But the program just terminates. I’ve found out, debugging the program, that the error occurs on the first line after the for loop. So I’m wondering if it is possible to do it or it’s something illegal.
This is because passing integral types that are less than
sizeof(int)bytes big to a varargs function will convert them up tosizeof(int)bytes when passed on the stack. IfAttributeLocationsmaller thansizeof(int)bytes, thensizeof(int)bytes are pushed on the stack, but you retrieve onlysizeof(AttributeLocation)bytes from the stack with theva_argcall. Then when you try to read thechar*, you get the rest of the bytes of theAttributeLocationand some of the bytes of theconst char*that you passed.