c++ - How to specify shared template variable with two template template parameters? -
i'm trying build mini test framework. have 1 function takes in 2 list-like variables composed of same type , plan on using template template parameters part of interface. far, have this,
template <typename c, template <class> class a, template <class> class b> static inline void assertequals(const a<c>& expected, const b<c>& actual) { auto success = 0, failure = 0; (auto iter1 = expected.cbegin(), iter2 = actual.cbegin(); iter1 != expected.cend() && iter2 != actual.cend(); ++iter1, ++iter2) { if (test::assertequals<c>(*iter1, *iter2)) { ++success; } else { ++failure; } } cout << "success: " << success << endl << "failure: " << failure << endl; }
the assertequals
in if condition function. question is, interface correct? secondly, how use it? i've tried no avail,
test::assertequals<int, std::vector, std::vector>(haystack, needle);
test::
class function resides in , haystack
, needle
of type std::vector<int>
.
your interface won't accept std::vector
, because takes 2 template arguments: contained type , allocator.
you change template signature accept variadic template templates, this:
template <typename c, template <class...> class a, template <class...> class b> static inline void assertequals(const a<c>& expected, const b<c>& actual) {
or set default parameter explicitly:
template <typename c, template <class, class = std::allocator<c>> class a, template <class, class = std::allocator<c>> class b> void assertequals(const a<c>& expected, const b<c>& actual) {
however, you're better off getting c
stl container member types, this:
template <class a, class b> void assertequals(const a& expected, const b& actual) { static_assert(std::is_same<typename a::value_type, typename b::value_type>::value, "containers must have same value type"); using c = typename a::value_type; //...
this more simple interface. of above options, compiler can deduce template arguments you, call like:
assertequals(haystack, needle);