Avoid storing std::span as class members
std::span is a non-owning view that doesn’t manage the lifetime of the data it references. Storing a span as a member creates a high risk of dangling references.
class RiskyClass { std::span<int> data_; // Dangerous!public: RiskyClass(const std::vector<int>& vec) : data_(vec) {} // What happens when 'vec' goes out of scope?};Better alternatives
Section titled “Better alternatives”- Store the actual container
- Take span as function parameters only
- Use span for temporary operations within a single scope