Created
April 2, 2017 16:08
-
-
Save ognian-/c8aff5cb6d4ac9a3ceea78fd5c50bb8c to your computer and use it in GitHub Desktop.
Converting errno to exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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