Proposal: allow C++17 fold expressions

61 views
Skip to first unread message

Daniel Cheng

unread,
Jul 3, 2022, 3:18:17 PM7/3/22
to cxx
Using fold expressions can avoid the need to write helpers to unpack the args one at a time.

For example, this:

#include <utility>

template <typename... Args>
int Sum(Args&&... args);

template <typename T, typename... Args>
int Sum(const T& val, Args&&... args) {
  return val + Sum(std::forward<Args>(args)...);
}

template <typename T>
int Sum(const T& val) {
  return val;
}

int main() {
  return Sum(1, 2, 3, 4);
}

Can be simplified into:

template <typename... Args>
int Sum(Args&&... args) {
  return (args + ...);
}

int main() {
  return Sum(1, 2, 3, 4);
}


Daniel

dan...@chromium.org

unread,
Jul 5, 2022, 9:32:35 AM7/5/22
to Daniel Cheng, cxx
LGTM

--
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 view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAF3XrKo8Wr_wzwYdvcn-mTtqZPpQ75vEO%2BsYkLdZoM9djDoOpg%40mail.gmail.com.

Kyle Charbonneau

unread,
Jul 5, 2022, 9:50:33 AM7/5/22
to Dana Jansens, Daniel Cheng, cxx
+1, the second snippet of code is much easier to understand.


Anton Bikineev

unread,
Jul 5, 2022, 11:08:57 AM7/5/22
to Kyle Charbonneau, Dana Jansens, Daniel Cheng, cxx
+1 (I already used them in a test, wrongly assuming they were allowed :) )

Roland Bock

unread,
Jul 5, 2022, 11:12:42 AM7/5/22
to Kyle Charbonneau, Dana Jansens, Daniel Cheng, cxx
+1 for allowing fold expressions.

Awesome for avoiding recursion in TMP.

On Tue, Jul 5, 2022 at 3:50 PM Kyle Charbonneau <kyle...@chromium.org> wrote:


--
--

Roland Bock

Software Engineering Manager

rb...@google.com


Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Liana Sebastian

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.


The above terms reflect a potential business arrangement, are provided solely as a basis for further discussion, and are not intended to be and do not constitute a legally binding obligation. No legally binding obligations will be created, implied, or inferred until an agreement in final form is executed in writing by all parties involved.

Daniel Cheng

unread,
Jul 5, 2022, 6:18:09 PM7/5/22
to Roland Bock, Kyle Charbonneau, Dana Jansens, cxx

Daniel

Mark Mentovai

unread,
Jul 5, 2022, 6:36:39 PM7/5/22
to Daniel Cheng, Roland Bock, Kyle Charbonneau, Dana Jansens, cxx
I endorse this too, but maybe give it another day for opinions to trickle in, so that this isn’t opened and driven entirely to completion entirely within a holiday weekend in the US.

Mark

Avi Drissman

unread,
Jul 5, 2022, 8:16:21 PM7/5/22
to Mark Mentovai, Daniel Cheng, Roland Bock, Kyle Charbonneau, Dana Jansens, cxx
I think this is good as well, but +1 to waiting a day or two more.

Daniel Cheng

unread,
Jul 7, 2022, 7:13:44 PM7/7/22
to Avi Drissman, Mark Mentovai, Roland Bock, Kyle Charbonneau, Dana Jansens, cxx
I'm going to go ahead and submit, since there hasn't been any further feedback that we should not allow this (and all the previous responses were positive).

Daniel
Reply all
Reply to author
Forward
0 new messages