2

This method:

void LRU::displayQueue() const
{
   for(iter = m_buffer.begin(); iter != m_buffer.end(); ++iter)
      std::cout << (*iter) << " ";
   std:: cout << std::endl;
}

results in the following error:

lru.cpp:58: error: passing 'const std::_Deque_iterator<int, const int&, const int*>' as    'this' argument of 'std::_Deque_iterator<int, const int&, const int*>& std::_Deque_iterator<int, const int&, const int*>::operator=(const std::_Deque_iterator<int, const int&, const int*>&)' discards qualifiers

m_buffer and iter are declared in my header file, where the buffer is declared as a deque of type int and iter is a constant iterator:

// ...

std::deque<int> m_buffer;
std::deque<int>::const_iterator iter;

// ...

Removing the const in the displayQueue method will eliminate the compiler error, but since this function shouldn't modify any data in the deque, I want to make this explicit by keeping my code "const-correct". Why would this result in an error, when my iterator is a const_iterator?

5
  • 2
    Is iter a member of LRU? If so, then that is a problem.
    – hmjd
    Nov 15, 2012 at 19:56
  • Is iter is LRU class member? If so you cannot modify it in const function unless you declare it mutable.
    – Rost
    Nov 15, 2012 at 19:56
  • try cbegin() and cend() to get a const iterator. Nov 15, 2012 at 19:56
  • @CaptainObvlious: That's not the problem here. Nov 15, 2012 at 20:00
  • That was a small "derp" moment for me. Thanks guys.
    – dtg
    Nov 15, 2012 at 20:09

5 Answers 5

5

You cannot modify any members of a class in a const member method, and that's what you're trying to do here:

for(iter = m_buffer.begin();iter != m_buffer.end(); ++iter)
    ^^^^^^^                                         ^^^^^^

iter is a member of class LRU, so you can't assign, increment or otherwise change it in a const member function.

One solution would be to make iter mutable:

mutable std::deque<int>::const_iterator iter;

But my feeling is most uses of mutable are an indication of a design flaw. In this case, I'd suspect that having iter be a member of class LRU in the first place is a potential design flaw. Why would you need/want this?

So, instead of making iter a mutable member, I'd first consider not having it be a member at all and use a local variable instead.

2

Because your member function is a const, which means you promise not to change any members in this function. Well, iter is a member and you are trying to change it in the function, so there is the error.

1

You're modifying the object by setting the value of your const iterator member variable, thus invalidating the const restriction of your function. Use a local variable.

A const_iterator only means the iterator cannot modify the sequence, but you're not modifying the sequence, you modifying the member variable iterator itself.

1

In a const object of type LRU, the iter member is const, so you can't modify it in that loop. You need to make a separate copy of it. That's why standard library algorithms take iterator objects: so they can modify the iterator objects to move through the sequence, even if there's an underlying container object that's const.

1
void LRU::displayQueue() const
{
  // std::_Deque_iterator<int, const int&, const int*> l_iter = m_buffer.begin();
  for(auto l_iter = m_buffer.begin(); l_iter != m_buffer.end(); ++l_iter)
    std::cout << (*l_iter) << " ";
  std::cout << std::endl;
}

should work. As other people said, a const value cannot be modified. The begin() method on the Deque class is overloaded to return const or non const values, and in this case it would pick up the non const one. Alternatively, you could explicitely denote the type of iter as not being const.

2
  • Yes, this should work, but it does not explain what the problem is or why this fixes it. It also introduces a potential new problem: name hiding. Nov 15, 2012 at 20:01
  • 1
    what type will auto deduce in this case? Mar 15, 2016 at 18:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.