// https://cirosantilli.com/linux-kernel-module-cheat#cpp-compile-time-magic #if __cplusplus >= 201703L #include #include template struct MyClass { MyClass() : myVar{0} {} void modifyIfNotConst() { if constexpr(!isconst) { myVar = 1; } } T myVar; static constexpr bool isconst = std::is_const::value; }; #endif int main() { #if __cplusplus >= 201703L MyClass x; MyClass y; x.modifyIfNotConst(); y.modifyIfNotConst(); assert(x.myVar == 1); assert(y.myVar == 0); #endif }