Propagate the invalid bit from bases to derived template classes.

Fixes PR16292.