Fixed #4767 (segmentation fault in expand template)