Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded [] operator on template class in C++ with const / nonconst versions

Whew, that was a long title.

Here's my problem. I've got a template class in C++ and I'm overloading the [] operator. I have both a const and a non-const version, with the non-const version returning by reference so that items in the class can be changed as so:

myobject[1] = myvalue;

This all works until I use a boolean as the template parameter. Here's a full example that shows the error:

#include <string>
#include <vector>
using namespace std;

template <class T>
class MyClass
{
    private:
        vector<T> _items;

    public:

        void add(T item)
        {
            _items.push_back(item); 
        }

        const T operator[](int idx) const
        {
            return _items[idx];
        }

        T& operator[](int idx)
        {
            return _items[idx];
        }

};


int main(int argc, char** argv)
{
    MyClass<string> Test1;      //  Works
    Test1.add("hi");
    Test1.add("how are");
    Test1[1] = "you?";


    MyClass<int> Test2;         //  Also works
    Test2.add(1);
    Test2.add(2);
    Test2[1] = 3;


    MyClass<bool> Test3;        // Works up until...
    Test3.add(true);
    Test3.add(true);
    Test3[1] = false;           // ...this point. :(

    return 0;
}

The error is a compiler error and the message is:

error: invalid initialization of non-const reference of type ‘bool&’ from a temporary of type ‘std::_Bit_reference’

I've read up and found that STL uses some temporary data types, but I don't understand why it works with everything except a bool.

Any help on this would be appreciated.

like image 419
riwalk Avatar asked Aug 11 '10 13:08

riwalk


People also ask

Can [] operator be overloaded?

An overloaded operator (except for the function call operator) cannot have default arguments or an ellipsis in the argument list. You must declare the overloaded = , [] , () , and -> operators as nonstatic member functions to ensure that they receive lvalues as their first operands.

What is const in operator overloading?

Overloading on the basis of const type can be useful when a function returns a reference or pointer. We can make one function const, that returns a const reference or const pointer, and another non-const function, that returns a non-const reference or pointer.

What is operator const in c++?

A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.


1 Answers

Because vector<bool> is specialized in STL, and does not actually meet the requirements of a standard container.

Herb Sutter talks about it more in a GOTW article: http://www.gotw.ca/gotw/050.htm

like image 63
Fred Larson Avatar answered Sep 22 '22 01:09

Fred Larson