//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  http://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  http://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <iostream>
#include <vector>

template<typename ValT>
class Collection {
 private:
  std::vector<ValT> values;
 public:
  Collection (const auto&... vals)
   : values{vals...} {
  }

  // overload subscript operator for const and non-const version:
  const auto& operator[] (int idx) const {
    return values[idx];
  }
  auto& operator[] (int idx) {
    return values[idx];
  }
  //...
};

int main()
{
  Collection<int> coll{1, 2, 3, 4, 5};
  const Collection<int> ccoll{1, 2, 3, 4, 5};

  coll[2] = 42;      // OK
  //ccoll[2] = 42;   // ERROR (good)

  std::cout << coll[2] << '\n';
  std::cout << ccoll[2] << '\n';  

}

