Not. For example, consider the following:
int badfunc(int &a, int &b) { return func(a++, b++); }
This behavior is undefined if a and b have the same referand. In the general case, the compiler cannot know what arguments will be passed to the function, so it cannot reliably recognize this case of undefined behavior. Therefore, it cannot catch all undefined behavior.
Compiler warnings are used to identify some instances of undefined behavior, but not all.
In theory, you could write an implementation in C ++ that will conduct a huge number of checks at runtime to ensure that undefined behavior is always identified and treated in the ways defined by this implementation. It still wonโt tell you at compile time (see: Stopping problem), and in practice, you will probably be better off with C #, which was designed to make the necessary runtime checks reasonably efficient ...
Even if you built this magic test of C ++ implementation, it still may not tell you what you really want to know, namely, whether your code is correct. Sometimes (hovering in your seats) is determined by the implementation whether the behavior is undefined. For a simple example, tolower((char)-1); defined the behavior [*] if char not specified, but undefined if char signed.
Thus, if your magic check does not do all the implementation options, like the โrealโ implementation that you want your code to work for, it will not tell you if the code defined the behavior for the set of implementation options in the โrealโ implementation, only if she defined the behavior for the implementation options made in the implementation of the magic check.
To find out that your code is correct and portable, you need to know (for starters) that it does not create undefined behavior for any set of implementation options. And, for that matter, for any input, and not just for the input used in your tests. You might think that this is a big flaw in C ++ compared to languages โโwithout undefined behavior. Of course, sometimes this is inconvenient and affects how you go about security software programs. In practice, however, in order for you to correctly evaluate your code, you do not just need it to determine the behavior, you need the behavior to match the specification document. This is a much more serious problem, and in practice it is more difficult to write an error in (say) Java or Python than in C ++. I wrote a lot of errors in all three, and knowing that in Java or Python the behavior was defined, but the wrong one did not help me with this.
[*] Well, the result is still determined by the implementation, it depends on the execution character set, but the implementation should return the correct result. If char signed, failure is allowed.