C++17 Generic Function Trap
Generic Function to Insert a String Element template <typename T, typename Coll> void insertAsString(T x, Coll& coll) { if constexpr(std::is_arithmetic<T>::value) { // OK since C++17 coll.push_back(std::to_string(x)); } else { coll.push_back(x); } } std::vector<std::string> values; insertAsString("hello", values); // OK insertAsString(42, values); // OK insertAsString(0.7, values); // OK 26
A photographed presentation slide shows a C++17 template function that inserts either an arithmetic value converted to a string or an existing string into a vector. The joke is the classic C++ trap: a seemingly generic function relies on compile-time branching and type traits, making a simple operation look surprisingly complicated and error-prone. It is shared as a reaction to intimidating or overly tricky C++ code. EN: A photographed presentation slide shows a C++17 template function that inserts either an arithmetic value converted to a string or an existing string into a vector. The joke is the classic C++ trap: a seemingly generic function relies on compile-time branching and type traits, making a simple operation look surprisingly complicated and error-prone. It is shared as a reaction to intimidating or overly tricky C++ code.
- @ithueti / 828 2019-04-19
- Type
- image
- Size
- 800×600 · 37.9 KiB
- Mood
- technical, ironic
- Language
- en
- Added