-2

Please see the following code:

if((pos = find()) != _sym.end())
{
    // do stuff
}

pos is a scoped variable of type std::vector<T>::const_iterator where T is a POD-struct containing a pair of the same type of iterator.

_sym is a private class member variable of type std::vector<T>.

The example code throws a runtime assertion error with the message Expression: vector iterators incompatible under Visual Studio 2013. The error occurs in <vector> at line 240.

On the other hand, if I re-write the code:

pos = find();
if(pos != _sym.end()) // do stuff

then, disco.

I'm fairly sure that comparing an initialized iterator (such as end()) with an uninitialized iterator causes this assertion; What I don't understand is if/how pos is not considered in an initialized state when the comparison operator is executed.

Jesse Hallam
  • 6,794
  • 8
  • 48
  • 70
  • 1
    The code you showed is irrelevant. Please prepare a code that reproduces the problem. – Vlad from Moscow Mar 25 '14 at 17:06
  • Have you tried other compiler? Providing SSCCE would be nice. It might be a VS bug/thingy. I'd say VS is known to have troubles occasionally with similar stuff. I think I saw two occasions it refused to compile proper code which was a bit convoluted. – luk32 Mar 25 '14 at 17:06
  • 1
    If might be an order of operations. In the first one, `_sym.end()` can be called before `find()`, where as in the second one, `find()` will always be called first. – Jeffery Thomas Mar 25 '14 at 17:07
  • What does find look like? If I try your code with std::find in VS2013 I don't get any errors. – Maurice Gilden Mar 25 '14 at 17:11
  • This might be relevant. http://stackoverflow.com/questions/15855634/why-am-i-getting-vector-iterators-incompatible I don't suppose find() changes _sym.end()? – QuestionC Mar 25 '14 at 17:16
  • @QuestionC: Good call, an edge case is calling another function which push_backs my container. Should have been a `const` function which would have illuminated the issue. Make an answer so I can give you credit. – Jesse Hallam Mar 25 '14 at 17:22

1 Answers1

1

This might be relevant.

Why am I getting "vector iterators incompatible"?

I don't suppose find() changes _sym.end()?

Community
  • 1
  • 1
QuestionC
  • 10,006
  • 4
  • 26
  • 44