4

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That works! Thank you! This is missing in the book and should be added to the errata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.