I'm experimenting with using C++'s template template features to reduce code duplication in a small unit test segment in my code, to no success. I've seen these answers to similar questions, but still can't figure out what my compiler is telling me.
I deal with some classes that do numerical processing at different precisions, and so I thought that I could generalize the duplicate code to a templated function, so it could be easily called by the class tests, as in:
template<typename T, size_t S>
void CompareArrays(
std::array<T, S> const &input,
std::array<T, S> const &output) {...}
template <typename T>
void SomeClassTest::SomeClassIdentity() const {
SomeClass<T> scZero;
std::array<T, 1> const input = { 1 };
auto output = scZero.Process(input);
CompareArrays(input, output); // does the actual printing
}
And then, test a lot of operations similar to SomeClassTest::SomeClassIdentity
with a template template function:
template<template <typename> typename F>
void CheckAgainstNumericTypes() {
std::cerr << "Testing with char...";
F<char>();
std::cerr << "Testing with short...";
F<short>();
std::cerr << "Testing with int...";
F<int>();
std::cerr << "Testing with float...";
F<float>();
std::cerr << "Testing with double...";
F<double>();
}
The problem is, every time I try to invoke CheckAgainstNumericTypes
, the compiler will refuse with the error message "Invalid Template Argument for 'F', type expected", as in the example below:
void SomeClassTest::Test() const {
std::cerr << "Some Class Tests #1 - base/identity case" << std::endl;
CheckAgainstNumericTypes<SomeClassIdentity>();
...
I tried making CheckAgainstNumericTypes
a member function of SomeClass
, prepending the template argument with SomeClass::
, adding ()
to the end of it, and even replacing the inner typedef
by void(*F)(void)
; all to no avail.
I have two questions, then:
- How can I transform my member function into a type so it is accepted by the template?
- Is there any other way of accomplishing the same desired syntactic result in
SomeClassTest::Tests()
without using template templates?