Skip to content

Instantly share code, notes, and snippets.

@ognian-
Created April 2, 2017 16:08
Show Gist options
  • Save ognian-/c8aff5cb6d4ac9a3ceea78fd5c50bb8c to your computer and use it in GitHub Desktop.
Save ognian-/c8aff5cb6d4ac9a3ceea78fd5c50bb8c to your computer and use it in GitHub Desktop.
Converting errno to exception
#include <cerrno>
#include <system_error>
#include <type_traits>
void check_errno() {
const auto e = errno;
if (e) {
errno = 0;
throw std::system_error(e, std::system_category());
}
}
template <typename Function, typename ... Args>
auto call_system(Function function, Args ... args) -> typename std::result_of<Function(Args ...)>::type {
const auto ret = function(args ...);
check_errno();
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment