Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
M9-3-1 and std::span-like classes
#1
Hi,

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

Code:
template <typename T>
class Span
{
public:
   Span(T* const ptr, std::size_t size) :
      data_{ptr},
      size_{size}
   {}

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

private:
   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?

Thanks!
Reply
#2
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")
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#3
Thanks for the clarification!
Reply
#4
This topic is now closed.

For discussion on related topics, please start a new thread
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)