Templated join function.