Tomilov Anatoliy
Tomilov Anatoliy

Reputation: 16711

Explicitly specify defaulted template parameter located after template parameter pack

Why can't I explicitly specify d in following case?

#include <iostream>

template< typename a, typename b = int, typename ...c, typename d = int >
int
f(a, b, c..., d)
{
    return sizeof...(c);
}

int
main()
{
    std::cout << f< int, int, int, int/*, char*/ >(1, 2, 3, 4, 'd') << std::endl;
    return 0;
}

If I uncomment last template argument, then I expect output 2, but instead I get a hard error:

main.cpp:14:18: error: no matching function for call to 'f'
    std::cout << f< int, int, int, int, char >(1, 2, 3, 4, 'd') << std::endl;
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:1: note: candidate function not viable: requires 6 arguments, but 5 were provided
f(a, b, c..., d)
^
1 error generated.

What is the rule to deny it in this case?

Upvotes: 1

Views: 84

Answers (1)

Edward Strange
Edward Strange

Reputation: 40859

Because packs are greedy. So char is actually part of c and you're expected to supply the argument associated with d, which is of type int due to the default.

Upvotes: 6

Related Questions