Joint evolution of specialization and dispersal in structured metapopulations