C++ 中 enable_if 模板 class 函数的覆盖率
coverage for enable_if template class functions in C++
我正在尝试为我的模板化数学 classes 生成准确的代码覆盖率。我默认使用 here
中的技巧实例化模板 class 中的每个方法
template class Vector2D<float>;
所以覆盖率并不总是 100%,但向我展示了从未调用过函数的地方。问题是,如果我更进一步并使用 type traits 仅针对某些类型启用模板化 classes 的成员函数,那么这些的覆盖率再次始终为 100%。 gcov 和 llvm-cov 都表明没有生成这些函数。 我猜这是因为这些函数在我的模板 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) {
...
}
};
我如何(默认)实例化这些函数,如果它们从未被调用,覆盖率报告将在 orange/red 中显示它们?
和classes一样,可以显式实例化functions/methods:
template
typename std::enable_if<std::is_floating_point<F>::value, void>::type
Vector2D<float>::rotate<float>(float);
使用 C++20 requires
,使用:
template<typename T>
class Vector2D {
//...
void rotate(T angle) requires(std::is_floating_point<T>::value) {
//...
}
};
我希望只需要显式 class 实例化。
我最终显式实例化了模板成员函数(参见 this post)
template void Vector2D<float>::rotate<float>(float);
如果我确实没有在代码中显式调用它,那么覆盖率报告会显示该函数从未调用过 (orange/red)。
我正在尝试为我的模板化数学 classes 生成准确的代码覆盖率。我默认使用 here
中的技巧实例化模板 class 中的每个方法template class Vector2D<float>;
所以覆盖率并不总是 100%,但向我展示了从未调用过函数的地方。问题是,如果我更进一步并使用 type traits 仅针对某些类型启用模板化 classes 的成员函数,那么这些的覆盖率再次始终为 100%。 gcov 和 llvm-cov 都表明没有生成这些函数。 我猜这是因为这些函数在我的模板 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) {
...
}
};
我如何(默认)实例化这些函数,如果它们从未被调用,覆盖率报告将在 orange/red 中显示它们?
和classes一样,可以显式实例化functions/methods:
template
typename std::enable_if<std::is_floating_point<F>::value, void>::type
Vector2D<float>::rotate<float>(float);
使用 C++20 requires
,使用:
template<typename T>
class Vector2D {
//...
void rotate(T angle) requires(std::is_floating_point<T>::value) {
//...
}
};
我希望只需要显式 class 实例化。
我最终显式实例化了模板成员函数(参见 this post)
template void Vector2D<float>::rotate<float>(float);
如果我确实没有在代码中显式调用它,那么覆盖率报告会显示该函数从未调用过 (orange/red)。