There was an earlier question related to this for C, but it comes down to a carefully written compiler test suite.
As to when compilers get the code wrong, I've hit that often enough in my professional career, thanks. It's happened less and less over time, but I found a bug in MS C++ compilers targeting CLI this week.