-
-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathif_constexpr.cpp
More file actions
29 lines (26 loc) · 606 Bytes
/
if_constexpr.cpp
File metadata and controls
29 lines (26 loc) · 606 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
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-compile-time-magic
#if __cplusplus >= 201703L
#include <cassert>
#include <type_traits>
template<typename T>
struct MyClass {
MyClass() : myVar{0} {}
void modifyIfNotConst() {
if constexpr(!isconst) {
myVar = 1;
}
}
T myVar;
static constexpr bool isconst = std::is_const<T>::value;
};
#endif
int main() {
#if __cplusplus >= 201703L
MyClass<double> x;
MyClass<const double> y;
x.modifyIfNotConst();
y.modifyIfNotConst();
assert(x.myVar == 1);
assert(y.myVar == 0);
#endif
}