I don't see any serious disadvantages, variables with meaningful names can help make the code much clearer. A decent optimizing compiler should be able to remove any overhead from the extra references in simple cases such as your example, but maybe not for more complex cases where the types are not identical (e.g. they have different const-qualification or require conversions).
There is another option that might be clearer in some cases: Instead of initializing a pair
with the result, you could create the variables you want, then create a pair
of references to those variables, and assign to them via the references:
int numTotal;
int NumSuccessful;
std::pair<int&, int&> result(numTotal, numSuccessful);
result = processSomething();
Or the same thing, without a named variable for the pair of references:
int numTotal;
int NumSuccessful;
std::pair<int&, int&>(numTotal, numSuccessful) = processSomething();
or in C++11 you can use the standard tie
function:
int numTotal;
int NumSuccessful;
std::tie(numTotal, numSuccessful) = processSomething();
A more unusual solution involves no temporary objects and allows you to make the variables const
, by using a local type with meaningful member names:
struct Num {
Num(std::pair<const int, const int> p) : total(p.first), successful(p.second) { }
int total;
int sucessful;
};
const Num num = processSomething();
std::cout << num.successful << '/' << num.total << '\n';