A "Singular Iterator" is defined as an:
iterators that are not associated with any sequence. A null pointer, as well as a default-constructed pointer (holding an indeterminate value) is singular
My question 1 would be: Is a default constructed iterator considered a "Singular Iterator"?
Secondly, I have been told here that:
Results of most expressions are undefined for singular values; the only exceptions are destroying an iterator that holds a singular value, the assignment of a non-singular value to an iterator that holds a singular value, and, for iterators that satisfy the DefaultConstructible requirements, using a value-initialized iterator as the source of a copy or move operation.
Question 2 is: Does working with a result that is "undefined" constitute Undefined Behavior? It would seem that if that were true this would be Undefined Behavior:
void* foo = nullptr;
auto bar = foo;
But it runs fine.
My deeper motivation for asking this question is in the case where I have a struct like this:
struct Foo {
vector<int*>::const_iterator;
};
I want to know if it is undefined behavior to do this, where assigned
is a value constructed Foo
object:
Foo unasigned;
assigned = unassigned;
If the answers to questions 1 and 2 are "yes" then by invoking the default assignment operator I am introducing undefined behavior :(
Question 2 is: Does working with a result that is "undefined" constitute Undefined Behavior?
Answer is: Definitely Yes. Working on UB is UB.
It seems to run fine for you since it is undefined. It can do anything, this includes working as expected, as well as working not as expected.
Regarding the example for the second question, it works fine because and is well-defined, because you don't actually dereference the pointer
foo
. The variablefoo
is initialized, all you are doing is initializing a variable with another initialized variable. It's no different from e.g.However, if you did e.g.
that would be UB because you dereference a null pointer.
Also, Undefined behavior is, well, undefined... It might seem to run fine but in reality it's really not.