if / else at compile time in C++?
C++17 if constexpr Oh yes, it has arrived: main.cpp #include <cassert> #include <type_traits> template<typename T> class MyClass { public: MyClass() : myVar{0} {} void modifyIfNotConst() { if constexpr(!isconst) { myVar = 1; } } T myVar; protected: static constexpr bool isconst = std::is_const<T>::value; }; int main() { MyClass<double> x; MyClass<const double> y; x.modifyIfNotConst(); y.modifyIfNotConst(); assert(x.myVar … Read more