MISRA Discussion Forums

Full Version: M9-3-1 and std::span-like classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Our static analyzer is reporting a violation of M9-3-1 in the following backport of C++20's `std:Confusedpan`:

template <typename T>
class Span
   Span(T* const ptr, std::size_t size) :

T* data() const { return data_; }  // M9-3-1 reported here

   T* data_;
   std::size_t size_;

int main()
    std::array<int, 3> arr{1,2,3};
    Span<int> s{arr.data(), arr.size()};

The `Span` class is merely a non-owning wrapper around a memory buffer. Furthermore, the `data()` function is not returning a reference to any class member, it's returning a copy of a class member. 

Would you say M9-3-1 is violated in this example?

The code doesn't violate the rule, as the value returned by the function does not allow any class member to be modified (see related question "Question about an example from rule 9-3-1")
Thanks for the clarification!
This topic is now closed.

For discussion on related topics, please start a new thread