Files
linux-kernel-module-cheat/userland/cpp/if_constexpr.cpp
Ciro Santilli 六四事件 法轮功 d09a0d97b8 learn more c++, it never ends
2020-03-19 00:00:00 +00:00

31 lines
588 B
C++

// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#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
}