Here's an example of what will cause the problem:
#include <deque>
int main() {
std::deque<int> has_data = {1, 2, 3};
std::deque<int>::iterator iter1 = has_data.begin() + 5; // This works fine
std::deque<int> had_data = {4, 5, 6};
had_data.clear();
std::deque<int>::iterator iter2 = had_data.begin() + 5; // This also works fine
std::deque<int> is_empty;
std::deque<int>::iterator iter3 = is_empty.begin() + 5; // This causes a segfault
}
Adding to an iterator of an empty deque only seems to be a problem if the deque has never contained any elements before.
I'm curious as to if this is a bug in the STL, or if I'm just using it in a way that causes undefined behavior. I only get this problem when compiling with Xcode (both the GUI and the command line). I have also tried it with GCC version 6.2.0 on Linux, but the problem didn't seem to exist there.