Posts

Showing posts from October, 2023

Template Metaprogramming

Among computer languages, C++ often gets described as having low-level functionalities while using high-level (somewhat, at least when compared to C ) computer-linguistic constructs. In what follows, please refer to the following example as a preface, template <typename __tp> struct __slow { public: using rx_type = __tp; public: inline static constexpr rx_type fox() noexcept(false) { return rx_type(); } }; This can be used as following, int main() { std::vector<int> some_vector; for(int i=1; i<10; ++i) { // one way to store all the even numbers is if(not (i & 1)) { some_vector.push_back(i); } // and the other, using the contrivance defined above (i & 1) ? __slow<void>::fox() : some_vector.push_back(i); } return 0; } The utility of this trick is somewhat of a clutter-remover, while writing algorithms mired with too many  if-else conditions. It has a simpl