0

I'm trying to generate accurate code coverage for my templated math classes. I'm default instantiating every method inside a template class with a trick from here

template class Vector2D<float>;

so that the coverage is not always 100% but shows me, where functions were never called. The problem is that if i go further and use type traits to enable member functions of templated classes only for certain types, the coverage for these is again always at 100%. Both gcov and llvm-cov show that these functions are not generated. I'm guessing it's because these function are their own "templates" within my templated class?

template<typename T>
class Vector2D {

...

template <class U = T>
typename std::enable_if<std::is_floating_point<U>::value, void>::type rotate(
T angle) {
    ...
    }
};

How can i (default) instantiate these functions such that the coverage report will show them in orange/red if they are never called?

mtosch
  • 351
  • 4
  • 18
  • Not sure to understand but... have you tried defining a `using` as `using foo_type = decltype(std::declval>().rotate(0.0f))` or something similar ? – max66 Sep 05 '20 at 10:59

2 Answers2

0

In the same way as classes, functions/methods can be explicitly instantiated:

template
typename std::enable_if<std::is_floating_point<F>::value, void>::type
Vector2D<float>::rotate<float>(float);

With C++20 requires, with:

template<typename T>
class Vector2D {

//...

    void rotate(T angle) requires(std::is_floating_point<T>::value) {
        //...
    }
};

I expect that only explicit class instantiation would be needed.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • not 100% what i need, but you got me on the right track. Thanks! i'll post my answer to the question for future reference – mtosch Sep 05 '20 at 20:14
  • It seems you can resolve the `std::enable_if`, so your answer is similar to mine. – Jarod42 Sep 06 '20 at 12:53
0

I ended up explicitly instantiating the template member function (see this post)

template void Vector2D<float>::rotate<float>(float);

The coverage report then shows the function as never called (orange/red) if i indeed do not call it explicitly in code.

mtosch
  • 351
  • 4
  • 18