The following piece of code does compile on gcc-4.7.1:
struct X {};
template <class T = X, typename U>
void f(const U& m) {
}
int main() {
f<>(0);
}
However, this one doesn’t:
struct X {};
template <class T = X, typename U>
void f(const U& m) {
auto g = [] () {};
}
int main() {
f<>(0);
}
gcc-4.7.1 complains:
c.cpp: In function 'void f(const U&)':
c.cpp:5:15: error: no default argument for 'U'
So my question is: is putting default parameters before non-default parameters correct in function template? If yes, why doesn’t the second one compile? If no, why does the first one compile? How does C++11 standard say about this syntax?
It is explicitly forbidden for classes and aliases. n3290 § 14.1.11 states:
For functions the only restriction seems to be related to parameter packs:
But clearly that doesn’t concern this case.
Given that nothing in § 14 forbids it for functions it seems we have to assume it is permitted.
A note from a working group reports seems to confirm that this is the intention. The original proposed wording of that section is:
I can’t see where that note went in the final version though.