-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpp_assert.hpp
More file actions
35 lines (29 loc) · 997 Bytes
/
cpp_assert.hpp
File metadata and controls
35 lines (29 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// cpp_assert.hpp : implement assert() macro in C++
// Version 2.0 2020/09/05, MIT license, (c) 2020 Richard Spencer
#ifndef cpp_assert_hpp
#define cpp_assert_hpp
#include <exception>
#include <iostream>
#include <string_view>
extern constexpr bool ReleaseBuild = false, AssertionsBuild = true;
template<typename T, typename E = std::exception>
inline void cpp_assert(T&& assertion, const E throwing = {}) {
if constexpr (AssertionsBuild) {
if (!assertion) {
throw throwing;
}
}
}
template<typename T>
inline void log_assert(T&& assertion,
const std::string_view log_msg = {},
std::ostream& out = std::cerr,
const char *file = __FILE__,
const int line = __LINE__) {
if constexpr (!ReleaseBuild) {
if (!assertion) {
out << file << '(' << line << "): *** assertion failed: " << log_msg << '\n';
}
}
}
#endif // cpp_assert_hpp