June 2017
Intermediate to advanced
532 pages
12h 59m
English
In order to relate how much constexpr-if constructs are an improvement to C++, we can have a look at how the same thing could have been implemented before C++17:
template <typename T>class addable{ T val;public: addable(T v) : val{v} {} template <typename U> std::enable_if_t<!std::is_same<T, std::vector<U>>::value, T> add(U x) const { return val + x; } template <typename U> std::enable_if_t<std::is_same<T, std::vector<U>>::value, std::vector<U>> add(U x) const { auto copy (val); for (auto &n : copy) { n += x; } return copy; }};
Without using constexpr-if, this class works for all different types we wished for, but it looks super complicated. How does it work?
The implementations alone of the two different add functions ...
Read now
Unlock full access