Assignment of a Singular Iterator

柔情痞子 提交于 2019-12-02 03:37:40

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 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!