mercredi 30 mars 2022

How can I initialize a custom Array class by aggregate initialization?

I have my own basic version of std::array

Here's how it looks:

template<typename T, size_t N> 
class Array {
  public:     
  Array()=default;
  T& operator[](size_t n) {return m_data[n];}     
  size_t Size() {return N;}
  private:     
  T m_data[N]; 
};

I can initialize it this way:

    Array<int, 3> arr;
    arr[0] = 11;
    arr[1] = 22;
    arr[2] = 33;

But what if I'd like to initialize it in aggregate, like this:

Array<int, 3> arr = { 1, 2, 3 };

How could I go about doing this?

Aucun commentaire:

Enregistrer un commentaire