   const correctness, etc for homebrew container
  Hi,I'm writing my own containers for the learning experience.  I'd like a quick reassurance that I am getting all the minutia correct, mainly const correctness and return types.  Here is my list declaration:template<typename T>class list{public://indexes may be negative, indicating reverse index (a la Python lists)const T& operator [](int index) const; //return item at an indexconst T& front() const; //first elementconst T& back() const; //last elementconst T& pop_front(); //pop first elementconst T& pop_back(); //pop last elementconst T& pop(int index); //delete and return specified index from listlist<T>& push_back(const T& el); //add an item to end of listlist<T>& push_front(const T& el); //add an item to front of listlist<T>& insert(const T& el, int index); //insert an itemlist<T>& remove(int index); //delete specified index from listlist<T>& clear(); //reset the list to empty statelist<T>& reverse(); //reverse the order of listlist<T>& insert_sorted(const T& el); //insert an item in sorted orderlist<T>& merge_sorted(list<T>& b); //merge two sorted lists into one sorted list by reusing nodeslist<T>& sort(); //sort list based on T::operator<//utility methodsconst bool empty() const; //true if list is emptyconst int length() const; //length of listconst bool operator ==(const list<T>& rhs) const; //check if contents are equallist();list(const list& rhs); //copy constructorprivate:const bool verify() const; //verify integrity of listunsigned int len; //current length of listpublic:node<T>* head;node<T>* tail;};Are there any design problems with this?  Is there ever a reason to return 'const list<T>&' or 'T&'?  I haven't written an iterator yet, hence the operator[].  I am reading right now in effective C++ that I need a 'const operator[](int index) const' as well as a non-const version.  Can someone explain why both versions are necessary?  Also, I would like to make each index const but it fails to compile, should I assume this is a design problem in my definition?   
  The expected behavior for operator[] is:const list → const access to elements.non-const list → const or non-const access to elements.Because the behavior is different, you need to include two versions, one for a const list, and one for a normal list.Also, what do you mean by not being able to compile with const indexes?   ;Quote:const list → const access to elements.non-const list → const or non-const access to elements.Would you elaborate on this please?  I don't understand what you mean by 'const or non-const access'.   ;Quote:Also, I would like to make each index const but it fails to compile, should I assume this is a design problem in my definition?If you're fiddling with 'int index' then the compiler will complain when using 'const int index' as a parameter. It should work if you're actualy handling the parameter as a constant within that function.Eventhough I'm not sure the compiler does any optimizations with that extra information, I sometimes use it --> void update(const float delta_time).Quote:Quote:const list → const access to elements.non-const list → const or non-const access to elements. Would you elaborate on this please? I don't understand what you mean by 'const or non-const access'.ToohrVyk tries to tell you that you should make an extra version of operator[]. One without the const thingies.;  You need two versions of op [] so the following vector code will work the same with your custom containervector<int> foo;const vector<int> bar;foo[1] = 1; //okayint x = foo[1]; //okayx = bar[1]; //okaybar[1] = x; //No! Bar is const!Assuming, of course, that foo and bar somehow magically aquire two elements.   ;Quote:Original post by thedustbustrQuote:const list → const access to elements.non-const list → const or non-const access to elements.Would you elaborate on this please?  I don't understand what you mean by 'const or non-const access'.For example:void funcition(list<int> &first, const list<int> second){   first[0] = 7;//Only works if there is a non-const [] because you can't modify const data   second[0] = 7;//should never work since second is const - const-returning [] makes sure of that   int num1 = first[0];//works with either []   int num2 = second[0];//Only works if there is a version of [] declared const};Const correctness: const overloading.