MISRA Discussion Forums
M9-3-1 and std::span-like classes - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.9 Classes (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=138)
+---- Thread: M9-3-1 and std::span-like classes (/showthread.php?tid=1600)



M9-3-1 and std::span-like classes - cgpzs - 18-02-2022

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!


RE: M9-3-1 and std::span-like classes - misra cpp - 23-02-2022

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")


RE: M9-3-1 and std::span-like classes - cgpzs - 23-02-2022

Thanks for the clarification!


RE: M9-3-1 and std::span-like classes - misra cpp - 13-05-2022

This topic is now closed.

For discussion on related topics, please start a new thread