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?