To return multiple values from a function using a compiler that supports C++17 you should do the following:
- Use an std::tuple for the return type.
std::tuple<int, std::string, double> find()
{
return std::make_tuple(1, "marius", 1234.5);
}
- Use structured bindings to unpack the values of the tuple into named objects.
auto [id, name, score] = find();
- Use decomposition declaration to bind the returned values to variables inside an if statement or switch statement.
if (auto [id, name, score] = find(); score > 1000)
{
std::cout << name << std::endl;
}