| CARVIEW |
Select Language
HTTP/2 200
date: Tue, 30 Dec 2025 13:53:20 GMT
content-type: text/html; charset=utf-8
server: cloudflare
last-modified: Mon, 05 Jul 2021 23:22:32 GMT
vary: Accept-Encoding
access-control-allow-origin: *
nel: {"report_to":"cf-nel","success_fraction":0.0,"max_age":604800}
expires: Tue, 30 Dec 2025 14:03:20 GMT
cache-control: max-age=600
report-to: {"group":"cf-nel","max_age":604800,"endpoints":[{"url":"https://a.nel.cloudflare.com/report/v4?s=Tsux9VXSerg0nPmBqdSR473u87aX4HAI5wXzUTvx9ygfPcI%2F1%2Fi8TbVlL44m4aKQp5erS54L5VdVmOsyvTNZAZkELbNjUCRyg6KqgVNI3w%3D%3D"}]}
x-proxy-cache: MISS
x-github-request-id: 37C7:22D4E:63CA548:6B439B3:6953D94F
cf-cache-status: DYNAMIC
content-encoding: gzip
cf-ray: 9b6205d069d9ffa1-BOM
alt-svc: h3=":443"; ma=86400
Return multiple values - C++ Patterns
← Patterns
Return multiple values
1234567891011121314 | #include <tuple>
std::tuple<int, bool, float> foo()
{
return {128, true, 1.5f};
}
int main()
{
std::tuple<int, bool, float> result = foo();
int value = std::get<0>(result);
auto [value1, value2, value3] = foo();
} |
This pattern is licensed under the CC0 Public Domain Dedication.
Intent
Return multiple values of different types from a function.
Description
The foo function on lines 3–6 returns a std::tuple
representing multiple values of different types.
On line 10, we call this function and store the result. We then get
the first of the returned values with std::get
on line 11.
Alternatively, on line 13 we use a structured binding declaration to declare and initialize variables with the returned values. The types of these variables are deduced automatically.
If the values are closely and logically related, consider composing
them into a struct or class type.