constexpr 构造函数不会显示覆盖率数据

constexpr constructor won't show coverage data

今天我将矩阵 class 重写为 constexpr。我对此 class 进行了 100% 的单元测试覆盖,但我注意到在将几乎所有函数转换为 constexpr 之后,构造函数的一部分在 lcov 中被标记为根本不再被覆盖。

这里是 class 只有构造函数。

template<typename T, std::size_t m, std::size_t n>
class Matrix
{
static_assert(std::is_arithmetic<T>::value,
                  "Matrix can only be declared with a type where "
                  "std::is_arithmetic is true.");

public:
    constexpr Matrix(
        std::initializer_list<std::initializer_list<T>> matrix_data)
    {
        if (matrix_data.size() != m)
        {
            throw std::invalid_argument("Invalid amount of rows.");
        }

        for (const auto& col : matrix_data)
        {
            if (col.size() != n)
            {
                throw std::invalid_argument("Invalid amount of columns.");
            }
        }


        std::size_t pos_i = 0;
        std::size_t pos_j = 0;

        for (auto i = matrix_data.begin(); i != matrix_data.end(); ++i)
        {
            for (auto j = i->begin(); j != i->end(); ++j)
            {
                this->data[pos_i][pos_j] = *j;
                ++pos_j;
            }
            ++pos_i;
            pos_j = 0;
        }
    }


private:
    std::array<std::array<T, n>, m> data{};

};


int main()
{
    Matrix<double, 2, 2> mat = {
        {1, 2},
        {3, 4}
    };

    return 0;
}

我正在使用 gcc 7.2 和 lcov 1.13

I have(had) 100% unit test coverage on this class but I noticed after I converted almost all functions to constexpr a part of the constructor is marked in the lcov as no longer covered at all.

lcov 指示 未覆盖代码 意味着 gcov 没有检测它。

标记为 constexpr 的任何内容都在 编译时进行评估gcov 覆盖率数据在 运行时.

这就是我怀疑的原因之一,为什么您没有获得任何 constexpr 函数的覆盖率数据。


因为你有模板代码,我不确定我是否是最新的,但我发现 gcov 不能很好地检测模板,你可能会得到零覆盖率数据为他们。

与我上面所说的 constexpr 类似的推理,模板在编译时是 evaluated/instantiated。至少很难以合理的方式检测所有实际使用的模板实例化。