Proposal to allow C++14 Return Type Deduction

56 views
Skip to first unread message

Taiju Tsuiki

unread,
Nov 30, 2017, 3:59:04 AM11/30/17
to cxx
Hi, C++ experts.

Return types of functions are sometimes hard to write and redundant, especially in template functions.
And C++14 Function Return Type Deduction makes the code much cleaner.

Regarding, it is currently To Be Discussed state on chromium-cpp, can we start using it?

Peter Kasting

unread,
Nov 30, 2017, 2:19:53 PM11/30/17
to Taiju Tsuiki, cxx
Will this improve readability?  In the example you link, I feel like declaring this as returning a callback type would be more verbose but maybe clearer.

The public Google style guide says nothing about this (but does say to use both auto and trailing return types only when they improve readability).  Dunno if the internal style mailing list has discussed this one.

PK

Taiju Tsuiki

unread,
Dec 1, 2017, 1:11:48 AM12/1/17
to Peter Kasting, cxx
Here is a comparison of the example with and without the deduction. I doubt that the `without` version is more readable than the other.

2017年12月1日(金) 4:19 'Peter Kasting' via cxx <c...@chromium.org>:
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFChzUzjrwESw5XQvyFOWG9g4tMLTYVA31cSsyAGsprJpg%40mail.gmail.com.

Peter Kasting

unread,
Dec 1, 2017, 1:18:27 AM12/1/17
to Taiju Tsuiki, cxx
On Thu, Nov 30, 2017 at 10:11 PM, Taiju Tsuiki <tz...@chromium.org> wrote:
Here is a comparison of the example with and without the deduction. I doubt that the `without` version is more readable than the other.

I actually like the "without" version better, but they're both kinda hard to grok :)

I kinda think we should allow this on principle since we have no good reason to ban it, but I don't know how to write guidance on when to use it.

PK

Jeremy Roman

unread,
Dec 1, 2017, 10:10:00 AM12/1/17
to Peter Kasting, Taiju Tsuiki, cxx
I'd be inclined to allow it with the usual "usage should be rare; only for heavily templated code" disclaimer to discourage people from indiscriminately deducing return types.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

dan...@chromium.org

unread,
Dec 1, 2017, 10:59:42 AM12/1/17
to Jeremy Roman, Peter Kasting, Taiju Tsuiki, cxx
Thanks for the example without. I think auto makes sense in this case. I was thinking guidance can be along the lines of "useful for complex templated return types" as well. Like Peter said, I don't think there's a reason to ban it but I don't think it's the right choice most of the time.

Jan Wilken Dörrie

unread,
Dec 20, 2017, 2:59:28 AM12/20/17
to dan...@chromium.org, Jeremy Roman, Peter Kasting, Taiju Tsuiki, cxx
Another example where automatic return type deduction improves readability is where the return type is simply the return expression wrapped in a decltype. An example from the codebase is the following (https://codesearch.chromium.org/chromium/src/base/bind_helpers.h?l=287-290):

template <typename T>
auto Unwrap(T&& o) -> decltype(Unwrapper<T>::Unwrap(std::forward<T>(o))) {
return Unwrapper<T>::Unwrap(std::forward<T>(o));
}

Using return type deduction this can be written as 
template <typename T>
decltype(auto) Unwrap(T&& o) {
return Unwrapper<T>::Unwrap(std::forward<T>(o));
}

which seems objectively better. However, these cases should be rare, as they only occur in heavily templated code. Thus I am also in favor of allowing this feature with the usual template disclaimer.

Peter Kasting

unread,
Mar 31, 2018, 12:31:40 PM3/31/18
to cxx
Reply all
Reply to author
Forward
0 new messages