What is the difference between undefined, unspecified, and implementation-defined behavior in C++?
Hello, colleagues! I've been writing in C++ for about six months, and I constantly come across the terms undefined behavior (UB), unspecified behavior, and implementation-defined behavior. I often read on forums that UB is a "scary thing" that can make a program "explode," but I don't fully understand the difference between these three concepts.
For example, I know that dereferencing a null pointer is UB. But what about the order of evaluation of function arguments? I wrote this code:
```cpp
#include
int foo() { std::cout << "foo "; return 1; }
int bar() { std::cout << "bar "; return 2; }
int main() {
int x = foo() + bar(); // What is the output order?
std::cout << x;
return 0;
}
```
With different compilers (GCC and Clang), I got different output: sometimes "foo bar 3", sometimes "bar foo 3". Wh
No answers yet.
Log in to leave an answer