// Thanassis Tsiodras, Dr.-Ing // My C++11 trick for common GLerror handling (regardless of whether the API returns something or not) // March 13, 2012 // User code: ... // this API (glCreateShader) returns something shader_ = GLERROR(glCreateShader(VertexShader==shaderType?GL_VERTEX_SHADER:GL_FRAGMENT_SHADER)); // this (glShaderSource) doesn't GLERROR(glShaderSource(shader_, 1, &pShaderCode, NULL)); // neither does this (glCompileShader) GLERROR(glCompileShader(shader_)); ... // Library code: how I did it with lambdas and std::function void commonCode(const char *file, size_t lineno) { GLenum err = glGetError(); if (err != GL_NO_ERROR) { while (err != GL_NO_ERROR) { std::cerr << "glError: " << (char *)gluErrorString(err) << " (" << file << ":" << lineno << ")" << std::endl; err = glGetError(); } throw "Failure in GLSL..."; } } template auto glError(std::function&& t, const char *file, size_t lineno) -> decltype(t()) { auto ret = t(); commonCode(file,lineno); return ret; } template<> void glError(std::function&& t, const char *file, size_t lineno) { t(); commonCode(file,lineno); } template auto helper (T&& t) -> std::function { std::function tmp = t; return tmp; } #define GLERROR( expr ) \ [&]() { \ return glError( helper( [&]() { return expr; } ), __FILE__, __LINE__); \ }()