Reputation: 95
I'm implementing some example from the book "A Tour of C++" by Bjarne Stroustrup. I have a template function to compute sum of elements (copied from the book):
template<typename Container, typename Value>
Value sum(const Container& c, Value v)
{
for (auto x : c)
v+=x;
return v;
}
I want to apply it to a template class Vector (defined in the book) which has a "begin" and "end" functions to support range-for loop (copied from the book):
template<typename T>
T∗ begin(Vector<T>& x)
{
return x.size() ? &x[0] : nullptr;
}
template<typename T>
T∗ end(Vector<T>& x)
{
return begin(x)+x.size();
}
when applied to Vector, for example :
Vector<int> v={1,2,3,4};
int s=sum(v, 0);
this lead to a compilation error. However, when I remove const from the sum template function
template<typename Container, typename Value>
Value sum( Container& c, Value v)
it compiles.
1- why it didn't work with the "const" ? If it is a problem with the "begin", "end" implementation, how to implement them?
Upvotes: 4
Views: 347
Reputation: 171127
Are these the only versions of begin
and end
? Notice they take the vector by reference to non-const
. To make sum
compile, you should also add const
overloads:
template<typename T>
const T∗ begin(const Vector<T>& x)
{
return x.size() ? &x[0] : nullptr;
}
template<typename T>
const T∗ end(const Vector<T>& x)
{
return begin(x)+x.size();
}
Note that this requires Vector
to have a const
overload of operator[]
. That's a very reasonable requirement; if the original Vector
doesn't have it (doubtful), you should add it.
Upvotes: 2