简体   繁体   中英

Repeatedly calling a function with a non-type template parameter

I have a function with a non-type template parameter of type int , like so:

template <int N>
int foo() { /*...*/ }

I would like to unit test this function for all values of N from 0 to 32. I have a function int expected(int n) that takes the same N value and returns the expected value. Effectively, I want:

if (foo<0>() != expected(0)) { /* fail... */ }
if (foo<1>() != expected(1)) { /* fail... */ }
if (foo<2>() != expected(2)) { /* fail... */ }
// 30 more lines

I don't want to write out all 33 test cases by hand, and I can't easily use a runtime loop because N is compile time.

How can I get the compiler to generate the test cases for me in a simple way, without BOOST_PP_REPEAT -style tricks or code generation, in C++11?

You can write a recursive function template with full specialization to perform the test. eg

template <int N>
void test() {
    test<N-1>();
    if (foo<N>() != expected(N)) { /* fail... */ }
}

template <>
void test<-1>() {
    // do nothing
}

and run it like

test<32>();

In c++14 you can do something like this

#include <type_traits>

template <int beg, int end> struct static_for {
    template <typename Fn> void operator()(Fn const& fn) const {
        if (beg < end) {
            fn(std::integral_constant<int, beg>());
            static_for<beg + 1, end>()(fn);
        }
    }
};

template <int n> struct static_for<n, n> {
    template <typename Fn> void operator()(Fn const& fn) const {}
};

template <int N> int foo() { /*...*/
    return N;
}

int main() {
    static_for<0, 32>()([&](auto i) {
        if (foo<i>() != i) { /* fail... */
        }
    });
    return 0;
}

Here's a method:

template<int N>
void f();

template<int... N>
void g(std::index_sequence<N...>)
{
  (f<N>(), ...);
}

Which can be called like so:

g(std::make_index_sequence<33>());

Edit:

Here's version that actually checks if the tests completed successfully:

template<int N>
int f();

int expected(int n);

template<int... N>
bool g(std::index_sequence<N...>)
{
  return ((f<N>() == expected(N)) && ...);
}

Which is used like:

g(std::make_index_sequence<33>()); // true if all tests are sucessful, false otherwise

A possible C++14 solution, that "simulate" the C++17 template-folding and interrupt the f<N> != expected(N) at first failure

template <int N>
void f ();

template <int ... Is>
void g (std::integer_sequence<int, Is...>)
 {
   using unused = int[];

   bool ret { false };

   (void)unused { 0, (ret ? 0 : (ret = (f<Is>() != expected(Is)), 0))... }; 
 }

callable as follows

g(std::make_integer_sequence<33>());

For a C++11 solution, you need a substitute for std::make_integer_sequence / std::integer_sequence that are available only from C++14.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM