3

We wrote a custom container class that should be able to support rang based for loop, see here: c++ shell. When you run the example you can see that it is working.

Essentially the container is a linked list of pointers to Elements:

    LinkedList ll;
    ll.push_back(new Element(1));
    ll.push_back(new Element(2));
    ll.push_back(new Element(3));

    for(LinkedList::iterator it = ll.begin(); it != ll.end(); ++it){
        cout << it->some_value << endl;
    }

    for(Element ele : ll) {
        cout << ele.some_value << endl;
    }

will print123123. What we don't understand is: Why are the ele in the range based for loop not pointers? Or better why does this not work:

for(Element * ele : ll) {
    cout << ele->some_value << endl;
}

Basically we want to achieve the same with the custom linked list as can be achieved with the std vector:

vector<Element*> vec{new Element(1),new Element(2),new Element(3)};
for(Element * ele : vec)
{
    cout<<ele->some_value<<endl;
}
4
  • Look at what LinkedList::begin() and LinkedList::end() return. Range based for loops will dereference those iterators for you. Note that each ele is a copy of the elements in ll. Use a reference to avoid that copy. To support your suggestion, you would need to be able to convert Element& to Element * implicitly. Commented Jun 19, 2017 at 19:20
  • Because Element ele is not a pointer? Commented Jun 19, 2017 at 19:21
  • I'm guessing you want for (const auto& ele : ... like one usually does... Commented Jun 19, 2017 at 19:22
  • No, what we really want is for(Element * ele : vec). I mean it is possible with the std vector, it should be possible with our custom linked list. Commented Jun 19, 2017 at 19:50

1 Answer 1

6

The standard defines the ranged based for to be equivalent to:

{
  auto && __range = range-init;
  for ( auto __begin = begin-expr,
             __end = end-expr;
        __begin != __end;
        ++__begin )
  {
    for-range-declaration = *__begin;
    statement
  }
}

The reason ele shouldn't be a pointer is because of the dereference of the iterator (for-range-declaration = *__begin;), so the for-range-declaration needs to be a reference type or the actual value type.

Hence you need to change your iterator class such that the operator* returns Element* or Element*&.

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

3 Comments

That is kind of a bit too abstract... what do we have to change such that for(Element * ele : ll) is working?
@newandlost: You need to change your iterator class such that operator* returns Element* or Element*&.
The way in which the anser is written like for-range-declaration = *__begin; is that c++ documentation language?

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.