Assignment of a Singular Iterator

后端 未结 2 1415
太阳男子
太阳男子 2021-01-27 03:06

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 (

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-27 04:10

    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 variable foo is initialized, all you are doing is initializing a variable with another initialized variable. It's no different from e.g.

    int foo = 0;
    auto bar = foo;
    

    However, if you did e.g.

    int* foo = nullptr;
    auto bar = *foo;
    

    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.

提交回复
热议问题