I have the following struct with a variadic constructor with a default argument:
struct S {
template<typename... Args>
S(int n=0, Args&& ...args) {}
};
int main() {
S s1; // fine on clang and g++ (S(0) by default argument, empty Args)
S s2(1); // fine on clang and g++ (S(1), empty Args)
S s3(1, 2); // fine on g++ (S(1, 2), non-empty Args), clang complains (see below)
}
As indicated, S(1, 2)
compiles fine on g++
(version 7.3, tested with flags -std=c++1z
and -std=c++14
), but clang
(version 6.0.0, tested with the same flags) tells me the following:
<source>:3:26: error: missing default argument on parameter 'args'
S(int n=0, Args&& ...args) {}
^
<source>:9:7: note: in instantiation of function template specialization 'S::S<int>' requested here
S s3(1, 2); // fine on g++ (S(1, 2), non-empty Args), clang complains (see below)
^
I already found Can parameter pack function arguments be defaulted? which deals with a similar problem, but covers only the case where the parameter list is completely empty (i.e. S s()
). I am particularly interested in the case S(1, 2)
.
The standard says:
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.
So I thought that my constructor for S
was valid and the values for n
and args
could be deduced in all three cases. Which compiler (if any) is correct?