C++: expand loop if types are different