Fix compilation with MSVC.

This patch is based on a patch provided by Yasin, slightly modified to
build on my system (the extra template keyword) and tested again by
Yasin.