Wrapping a call to a template function in a macro or excluding a void return specialization

I have the following function that allows me to exchange OpenGL commands and log when something goes wrong:

template<typename Res, typename Func, typename... Args>
struct Checker {
    static Res run(const std::string& function_name, Func&& func, Args&&... args) {
        Res result = func(std::forward<Args>(args)...);
        check_and_log_error(function_name);
        return result;
    }
};

template<typename Func, typename... Args>
struct Checker<void, Func, Args...> {
    static void run(const std::string& function_name, Func&& func, Args&&... args) {
        func(std::forward<Args>(args)...);
        check_and_log_error(function_name);
    }
};

template<typename Func>
struct Checker<void, Func> {
    static void run(const std::string& function_name, Func&& func) {
        func();
        check_and_log_error(function_name);
    }
};

}

template<typename Res=void, typename Func, typename... Args>
Res _GLCheck(const std::string& function_name, Func&& func, Args&&... args) {
    GLThreadCheck::check();
    return GLChecker::Checker<Res, Func, Args...>::run(function_name, std::forward<Func>(func), std::forward<Args>(args)...);
}

I would like to wrap _GLCheck in a macro so that the function name parameter will be provided automatically, for example.

#define GLCheck(...) _GLCheck(__func__, __VA_ARGS__)

This works fine provided that the GL call does not return a value, but if it does, then _GLCheck should be created this way:

program_object_ = _GLCheck<GLuint>(__func__, glCreateProgram);

Obviously, my macro fails when I need to specify the type of the return type. Is there any way:

  • Write the best macro to handle this situation, or ...
  • Avoid explicitly determining the return type by changing template functions?

Thank!

+4
2

, _GLCheck , Func. :

template<typename Func, typename... Args>
auto _GLCheck(const std::string& function_name, Func&& func, Args&&... args)
    -> decltype(func(args...)) {
// ...

decltype , , , .:)

+2

. - :

ObjectThatWillLogInItsDestructor obj(function_name);
return function_returning_void();
-1

All Articles