We can overload on constness of memberfunction




template <typename T, ... >
class vector
{

public:

    T&       operator[](size_t i);
    const T& operator[](size_t i) const;

};





STL is const-safe:





template <typename It, typename T>
It find( It begin, It end, const T& t)
{
    while (begin != end)
    {
        if ( *begin == t )
        {
            return begin;
        }
        ++begin;
    }
    return end;
}



const char t[] = { 1, 2, 3, 4, 5 };

const char *p = std::find( t, t+sizeof(t), 3)

if ( p )
{
    // syntax error: *p = 6;
}


const std::vector<int>  v(t, t+sizeof(t));

std::vector<int>::const_iterator i = std::find( v.begin(), v.end(), 3);

if ( v.end() != i )
{
    // syntax error: *i = 6;
}


//
//  C++11
//

std::vector<int>  v(4,5);
auto i = std::find( v.begin(), v.end(), 3);   // i is vector::iterator

const std::vector<int>  v(4,5);
auto j = std::find( v.begin(), v.end(), 3);   // j is vector::const_iterator


std::vector<int>  v(4,5);
auto k = std::find( v.cbegin(), v.cend(), 3); // k is vector::const_iterator


//
// issue in C++98:
//

std::vector<int>  v(3,5);
std::vector<int>::const_iterator ci = std::find( v.begin(), v.end(), 3);
v.insert(ci, 2);  // syntax error in C++98


std::vector<int>  v(3,5);
auto ci = std::find( v.cbegin(), v.cend(), 3);
v.insert(ci, 2);  // shoud work in C++11 but syntax error in my g++ 4.8
                  // works from g++ 4.9.2


//
//  C++11: use global begin() and end()
//

std::vector<int>  v(4,5);
auto i = std::find( begin(v), end(v), 3);   // i is vector::iterator


// fails in C++11
std::vector<int>  v(4,5);
auto i = std::find( cbegin(v), cend(v), 3); // C++11 failed to add cbegin, cend


// fixed in C++14
std::vector<int>  v(4,5);
auto i = std::find( cbegin(v), cend(v), 3);