Visual Studio const_iterator Assignment Error

前端 未结 1 1963
深忆病人
深忆病人 2021-01-22 04:01

The assignment of a default constructed vector::const_iterator errors on Visual Studio 2010. I\'ve tried this on 5 Visual Studio 2010 systems that all h

相关标签:
1条回答
  • 2021-01-22 04:15

    This code has undefined behavior. [iterator.requirements.general]/p6:

    Iterators can also have singular values that are not associated with any sequence. [ Example: After the declaration of an uninitialized pointer x (as with int* x;), x must always be assumed to have a singular value of a pointer. —end example ] 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. [ Note: This guarantee is not offered for default initialization, although the distinction only matters for types with trivial default constructors such as pointers or aggregates holding pointers. —end note ] In these cases the singular value is overwritten the same way as any other value. Dereferenceable values are always non-singular.

    uninitialized is singular, and its use doesn't fall within any of the exceptions listed in the paragraph.

    However, given the snippets you post, I suspect that your code wouldn't work either even if you value-initialize uninitialized, which is a bug in Microsoft's implementation, and which they fixed in a later hotfix.

    0 讨论(0)
提交回复
热议问题