tags:

views:

202

answers:

4

I came across this example here:

#include <vector>
#include <cstddef>

template<typename Tag>
class Ref_t {
   std::size_t value;

   friend Tag& element(Ref_t r, std::vector<Tag>& v) {
     return v[r.value];
   }

   friend const Tag& element(Ref_t r, const std::vector<Tag>& v)
   {
     return v[r.value];
   }
public:
   // C'tors, arithmetic operators, assignment

};

struct A{};
struct B{};

typedef Ref_t<A> ARef_t;
typedef Ref_t<B> BRef_t;

int main() {
   std::vector<A> va;
   ARef_t ar;
   A& a = element(ar, va);

}

So the question is why do we need -two friend element functions in Ref_t class?

A: 

You don't, you only need the non const version in this example

Patrick
See MSalters reply: the argument of the const version is itself const. If you want to call the function with a const parameter you need the const version.
Kristof Provost
@Kristoff - yes, your point being? The const version was not needed in the example given in the same way as the non const version is not needed in MSalters example.
Patrick
Yes, but this is supposed to be general-purpose class, and the question is particularly about situations where it would be needed.
UncleBens
+4  A: 

The difference between the two functions is that an element() of a non-const vector is itself non-const, but if the entire vector is const, then each element() is also const.

i.e.

int main() {
   std::vector<A> const cva = foo();
   ARef_t ar;
   A const& a = element(ar, cva);
}
MSalters
+1 Great... How could I not see it :)... Agh, it is 7:40 in USA
vehomzzz
+1  A: 

If you hold a const vector you can read the element values, but not change them. If you hold a non-const vector, then you can actually change the element values. In both cases you call the same function name, but a different function is invoked because of the overloading on const.

operator[] on the vector itself is implemented this way too.

In effect the non-const "getter" also acts as a "setter"

Phil Nash
A: 

Both function returns the element of respective vector sticking the behaviour of the elements of the vector.

i.e. if vector is const ==> its all elements are const ==> one cant't modify its element and for that function with const return type and const argument is defined. so it wont allow to modify the element of vector.

Exactly opposite is for non-const vector.One can modify the element of vector using its reference.

Ashish