3 ms·
> Side note: Someone brought up structured bindings in a comment, as if this fixed the issue. Structured bindings are a great example of the half-way fixes that
by nwallin 3y ago
> Side note: Someone brought up structured bindings in a comment, as if this fixed the issue. Structured bindings are a great example of the half-way fixes that proponents of modern C++ love to cite. Structured bindings help some, but if you think they make returning by tuple ergonomic, you’re mistaken. You still need to write std::pair or std::make_tuple in the function return statement.
No you don't. A tuple needs to be declared once. Then everything else is sugared.
#include <string>
#include <tuple>
using Foo = std::tuple<int, std::string, float>;
Foo foo(int a, const char* b, double c)
{
return {a,b,c};
}
Foo bar(long);
double baz(long x) {
const auto [a, b, c] = bar(x);
return c;
}
godbolt: https://godbolt.org/z/exdsb4hqb https://godbolt.org/z/exdsb4hqb
Note that the return part of this (foo()) worked as far back as C++11. Only the structured bindings on the receiving side (baz) requires C++17.
- thecodedmessage 3y agoThanks! It’s still not as ergonomic as full first class support, but I did have the situation in my head as worse than it was. This has been fixed :)