A C++ implementation that detects undefined behavior?

后端 未结 10 1366
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 14:13

A huge number of operations in C++ result in undefined behavior, where the spec is completely mute about what the program\'s behavior ought to be and allows for anything to

10条回答
  •  难免孤独
    2020-11-27 14:52

    Undefined behaviour is undefined. The best you can do is conform to the standard pedantically, as others have suggested, however, you can not test for what is undefined, because you don't know what it is. If you knew what it was and standards specified it, it would not be undefined.

    However, if you for some reason, do actually rely on what the standard says is undefined, and it results in a particular result, then you may choose to define it, and write some unit tests to confirm that for your particular build, it is defined. It is much better, however, to simply avoid undefined behaviour whenever possible.

提交回复
热议问题