简体   繁体   English

如何处理来自核心指南检查器关于 gsl::at 的静态分析警告?

[英]How to handle static analysis warning from Core Guidelines checker about gsl::at?

I activated static analysis for my project in Visual Studio.我在 Visual Studio 中为我的项目激活了静态分析。 The Core Guidelines checker says i should use gsl::at for subscription.核心指南检查器说我应该使用 gsl::at 进行订阅。 But my code is save.但我的代码是保存。 What's the cleanest way to get rid of this warning?摆脱这个警告的最干净的方法是什么? Should I disable it?我应该禁用它吗? Should I write my code differently?我应该以不同的方式编写代码吗? Should I use gsl::at introducing an overhead for the check?我应该使用 gsl::at 引入检查的开销吗? My code is:我的代码是:

template <typename T, size_t N>
void increase(T(&data)[N])
{
    for (size_t i = 0; i < N; ++i)
        data[i] +=1;
}

I found a solution myself.我自己找到了解决方案。 Because the size is known at compile time, it is possible to use a std::array, std::get and std::index_sequence.因为大小在编译时是已知的,所以可以使用 std::array、std::get 和 std::index_sequence。 std::get leads to a compilation error if the index is out of bounds.如果索引超出范围,则 std::get 会导致编译错误。 There is no longer a need for a runtime check.不再需要运行时检查。

template <typename... Ts>
constexpr void noop(Ts...) noexcept {}

template <typename T, size_t N, size_t... I>
constexpr void increase(std::array<T, N>& data, std::index_sequence<I...>) noexcept
{
    noop((std::get<I>(data) += 1)...);
}

template <typename T, size_t N>
constexpr void increase(std::array<T, N>& data) noexcept
{
    increase(data, std::make_index_sequence<N>{});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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