The compiler cannot optimize this to just return true, because it doesn't know if bar will const cast away
Realistically the missed optimizations in cases like this aren't as important as you might imagine. There are attributes (other than pure) that allow you toe more clearly specify how functions may or may not alter memory.