by diath · 3y ago · 0 comments
auto get_multiple_values(const bool condition)
-> std::tuple<bool, int>
{
if (condition) {
return {true, 5};
}
return {false, 0};
}
void foo()
{
auto [status, value] = get_multiple_values(true);
}For this reason, some prefer a “auto almost everywhere” style, in which you preferentially use trailing return types.
it is known as sticky syntax in relation to auto (but specifically when the type on the rhs is explicitly declared and not abstracted away with a function signature)
it removes narrowing, inference, while keeping benefits of auto related to inits etc.
it's a way of telling the compiler we want this type moving forward and adheres to the convention of right-to-left reading w.r.t. resource allocation semantics C++ programmers are often accustomed to.
Then once that was opened some folks felt it would be better to write their regular code that way too.
Ehh... not putting a return type in at all is a bridge to far IMO. Be friendly to the casual reader of the code, and help them understand what the function returns. (Yes I know IDE's can do this for you, but not everyone uses an IDE, and not everyone is reading the code in the context of an IDE... for instance code review, etc.)
Wrote this for myself, I’m a vim user :)
auto foo() {
struct Result { int a; int b; int c; };
return {1,2,3};
}
//....
auto [a,b,c] = foo();