#define vs template | SoloLearn: Learn to code for FREE!

+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]); }

5/22/2020 3:40:11 PM

Orville Vroemen

4 Answers

New Answer

+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.

+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.

+1

Thanks Mohamed ELomari, it worked with template <template<class...> class S, class T>, although Schindlabua's solution did work with std::set

+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