Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I pass a span of an array to a base class?

How can I pass a span of an array to a base class? If that’s not possible, how about passing a const ref to the array? The issue is that both the span and the array require an explicit size as template argument, so the base class needs that size. Currently, I’m doing this with a vector.

I’m trying to do something like:

enum Seasons {
  Spring, Summer, Fall, Winter
};

struct Base {
    template<size_t N>
    Base(std::span<Seasons, N> list) {}
};


struct Derrived : Base {
  inline static const std::array mValues = {
      Spring, Winter
  };

  Derrived() : Base(???) {}
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To deduce std::span template parameters you should call the constructor:

struct Base {
  template <std::size_t N>
  Base(std::span<const Seasons, N> list) {}
  //             ^^^^^
  // T must be const for const std::array
};

struct Derrived : Base {
  inline static const std::array mValues = {Spring, Winter};

  Derrived() : Base(std::span(mValues)) {}
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading