12

Consider the following minimal example:

#include<cstddef>

template<std::size_t... I>
constexpr auto sum() { return (I + ...); }

template<bool... B>
constexpr auto check() { return (B && ...); }

int main() {
    static_assert(6 == sum<1,2,3>(), "!");
    // static_assert(0 == sum<>(), "!");
    static_assert(check<true, true>(), "!");
    static_assert(check<>(), "!");
}

The commented line doesn't compile.
The same applies using * instead of +.
The one involving booleans works instead.

Here (working draft) I haven't found mentions about empty parameter packs.
On the other side, here (isocpp) it seems that the default result in the case above is int().

What's exactly the expected behavior when mixing fold expressions and an empty parameters pack?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
skypjack
  • 49,335
  • 19
  • 95
  • 187

1 Answers1

15

This is covered in [temp.variadic]¶9 (citing N4618):

If N is zero for a unary fold-expression, the value of the expression is shown in Table 14; if the operator is not listed in Table 14, the instantiation is ill-formed.

Table 14 — Value of folding empty sequences:

Operator  |  Value when parameter pack is empty  
-----------------------------------------------
&&        |  true
||        |  false
,         |  void()

The reason only these three operators are supported is outlined in P0036R0.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • I'd be curious to know why these operators were singled out as opposed to making them all ill-formed. – Borgleader Jul 25 '16 at 16:48
  • 5
    @Borgleader : See [P0036R0](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0036r0.pdf). However [P0160R0](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0160r0.html) says that defaults for empty parameter packs were resolved to be removed entirely; I'm not sure what changed or why this isn't reflected in the current draft. – ildjarn Jul 25 '16 at 16:51
  • 2
    @ildjarn That didn't get consensus in plenary, so eventually they voted P0036 in instead. – T.C. Jul 25 '16 at 17:23
  • 2
    @T.C. : Thank you! I see that now in the [Kona minutes](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4558.html) paper; for future reference, is there an easier source to see such vote results, or are the minutes papers the best place to look? – ildjarn Jul 25 '16 at 17:44
  • @downvoter : As per SO etiquette, if you're going to downvote an answer (which should only be if it's ["*egregiously sloppy, \[a\] no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect*"](https://stackoverflow.com/help/privileges/vote-down)), leave an explanation as to _why_ you're downvoting so that the answer can be improved if necessary. – ildjarn Jul 25 '16 at 22:45
  • @ildjarn Actually it doesn't make much sense to downvote your question. I'm voting it up to compensate. Can you integrate the answer with the links posted in the comments? I think they are useful to fully cover the topic. Thank you. – skypjack Jul 26 '16 at 05:48
  • 1
    @ildjarn The minutes is still the best publicly available place to look AFAIK. – T.C. Jul 26 '16 at 19:21
  • 1
    @ildjarn There are four users actually named downvoter. Which of them were you talking to and how did you know it was them that downvoted the answer? – Jerry Jeremiah Aug 08 '16 at 22:31