#define vs template | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

#define vs template

Hi guys, in this code I'm using #define s std::set, so I can easily switch to e.g. std::vector or std::list, I was just wondering if there's a way to do this with templates, to make the code a little cleaner and adaptable. Say I want to use the printcont() function for printing an std::set AND an std::vector without having to write the function twice, how would I do that? Without using #define. Please let me know! #include <set> #define s std::set template <typename T> void printcont(s<T> const &a) { typename s<T>::const_iterator itr; for (itr = a.begin(); itr != a.end(); ++itr) std::cout << *itr << "\n"; } template <typename T> void arrpush(s<T>& a, T b[], int size) { for (int i = 0; i < size; ++i) a.insert(b[i]); }

22nd May 2020, 3:40 PM
Orville Vroemen
Orville Vroemen - avatar
4 Answers
+ 2
the solution provided by @Schindlabua , will not work. the problem is that the template template argument says that it should have one argument, but the template that's passed as the actual argument may has two or more. ( the template passed as the argument must match the declaration of the template template argument ) formally: std::vector is → template <class T, class Allocator = std::allocator<T>> class vector { ... }; std::set is → template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class set { ... }; in order to use std::vector or std::set as the first template argument to "printcont", the definition of "printcont" needs to be changed: template <template<class,class...> class S, class T> void printcont(S<T> const& container) { ... } this way you can focus on the value type, but still maintain any allocator template parameters etc.
22nd May 2020, 9:03 PM
MO ELomari
+ 1
That's possible with "template template parameters"! Silly name I know. template <template<class> class S, class T> void printcont(S<T> const& container) { ... } C++20 "concepts" might be a better fit in most situations though.
22nd May 2020, 4:08 PM
Schindlabua
Schindlabua - avatar
+ 1
Thanks Mohamed ELomari, it worked with template <template<class...> class S, class T>, although Schindlabua's solution did work with std::set
24th May 2020, 8:10 AM
Orville Vroemen
Orville Vroemen - avatar
+ 1
only works for GCC but for clang and msvc will not work: CLANG → : https://godbolt.org/z/3s-Uxu MSVC → : https://godbolt.org/z/i76ETq
24th May 2020, 4:16 PM
MO ELomari