mercredi 28 janvier 2015

Can default function arguments "fill in" for expanded parameter packs?

The following code fails to compile :



#include <iostream>

template<typename F, typename ...Args>
static auto wrap(F func, Args&&... args)
{
return func(std::forward<Args>(args)...);
}

void f1(int, char, double)
{
std::cout << "do nada1\n";
}

void f2(int, char='a', double=0.)
{
std::cout << "do nada2\n";
}

int main()
{
wrap(f1, 1, 'a', 2.);
wrap(f2, 1, 'a');
}



g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(*)(int, char, double); Args = {int, char}]':
main.cpp:22:20: required from here
main.cpp:6:44: error: too few arguments to function
return func(std::forward<Args>(args)...);


It seems that the rule about "parameter packs being last" is followed (at least in the declaration) and after the expansion a correct function call should be formed : f2 can either be called with 1, 2 or 3 arguments so the too few arguments being an error seems 'harsh'. It also doesn't look like a deduction problem (which would be my guess - but got shaky due to the error message)


Is this a missing feature or there's a violation from the Standard's point of view ?


Aucun commentaire:

Enregistrer un commentaire