22

Maybe I'm missing something obvious, but the following compiles and runs, and I'm not sure why. I am aware of this, but in the example below the position of the parameter pack and the default argument are reversed. Doesn't it violate the rule that default arguments have to appear last? The parameter pack cannot have a default value.

#include <iostream>
#include <string>
#include <tuple>

template<typename ... Ts>
struct Test
{
    int i;
    std::string str;

    Test(int _i = 0, Ts&& ... _ts)
        :
          i(_i),
          str(std::get<0>(std::forward_as_tuple(std::forward<Ts>(_ts)...)))
    {}
};

int main()
{
    Test<std::string> t(1, "huh??");
    std::cout << "t.i = " << t.i << ", t.str = " << t.str << "\n";

    return 0;
}

This produces

t.i = 1, t.str = huh??
2
  • Isn't empty the default for any parameter pack?
    – Walter
    Commented Nov 16, 2017 at 22:23
  • yes, is empty the default value.. Commented Nov 17, 2017 at 4:53

3 Answers 3

22

From 8.3.6 ([dcl.fct.default])/4:

For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter pack. A default argument shall not be redefined by a later declaration (not even to the same value). [ Example:

void g(int = 0, ...); // OK, ellipsis is not a parameter. So it can follow a parameter with a default argument
10
  • 3
    (Nit picking) This does not answer the question (which was not "is it", but "why is it allowed").
    – Walter
    Commented Nov 16, 2017 at 22:28
  • 3
    @KyleStrand You do, but I don't. Rather I think the most sensible interpretation is why does the standard allow this? Note also that the question is not tagged language-lawyer. In general, the most sensible answer to why is such and such allowed is not because that's how the law is. Unless, perhaps you're a lawyer.
    – Walter
    Commented Nov 16, 2017 at 22:43
  • 1
    True, I accepted the answer based on the fact that this is the law, but now that you've mentioned it...in what cases would this be useful? I should probably ask a separate question...
    – cantordust
    Commented Nov 17, 2017 at 1:15
  • 1
    @cantordust To save you some trouble: "why" questions of that sort are often answered with some variation of "how should we know? You'd have to ask the standards committee". It's possible that there's some record of discussion between the members about whether parameter packs should be permitted in conjunction with optional arguments at all, but I wouldn't count on it. Commented Nov 17, 2017 at 6:12
  • 1
    Note, though, that requiring parameter packs to come before optional arguments wouldn't make sense, since there's be no clear rule for how the compiler would distinguish between the last parameter pack argument and the optional argument. Commented Nov 17, 2017 at 6:13
11

The why of it is simple. Effectively parameter packs always have a default: Parameter packs can be empty, so that won't contradict the concept that missing defaults must be the last arguments.

0
9

As an addition to the great answer by rsp, it's also worth noting that this behavior makes logical sense. Non-default, non-parameter-pack arguments cannot follow a default argument without resulting the in the requirement that the default argument has to be specified--in which case it is no longer a default argument.

For example, if the following were allowed:

void example(int x=0, int y);

The non-default second argument would mean a call to function would need to be structured example(1, 2); as the first parameter cannot be defaulted. This is not the case for an empty parameter pack. Consider the following function:

template <typename... T> void example(int x = 0, T&&... t);

In this case it is still possible to default x by calling example();

1
  • 2
    What's more, void example(int x=0, int y); is sometimes allowed: it's allowed if a prior declaration already established a default value for y. This matches the rest of your explanation: that's the one way in which it's possible for the default value for x to get used.
    – user743382
    Commented Nov 16, 2017 at 20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.