You want to compute the return type along with the return value, something a plain function cannot do. The alternative would be to use a standard function, a separate type lambda and casts. It might even end up being less ugly. I went straight for the correctly typed version and didn't attempt that.