KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Consider the following code : #include <iostream> #include <type_traits> template<typename T> class MyClass { public: MyClass() : myVar{0} {;} void testIf() { if (isconst) { myVar; } else { myVar = 3; } } void testTernary() { (isconst) ? (myVar) : (myVar = 3); } protected: static const bool isconst = std::is_const<T>::value; T myVar; }; int main() { MyClass<double> x; MyClass<const double> y; x.testIf(); x.testTernary(); y.testIf(); // <- ERROR y.testTernary(); // <- ERROR return 0; } For x (non-const) there is no problem. But y (const data type) cause an error even if the condition in if/else is known at compile-time. Is there any possibility to not compile the false condition at compile-time ?
Tags (comma-separated)
Save Edits
Cancel