Why can't default functions be redirected in C ++?

I found a rather strange behavior from my point of view: the function arguments by default cannot be redirected in the code below.

void Test(int test = int{}) {} template<typename F, typename ...Args> void Foo(F&& f, Args&&... args) { std::forward<F>(f)(std::forward<Args>(args)...); } int main() { Foo(Test, 0); // This compiles Foo(Test); // This doesn't compile } 

Clang reports: error: too few arguments to call the function, expected 1, there are 0 GCC and VC report the same errors.

Can someone explain this?

The code is here: http://rextester.com/live/JOCY22484

+7
c ++ c ++ 11 perfect-forwarding
source share
1 answer

Test is a function that always takes one argument. If its declaration with a default argument is displayed when Test is called by name, the compiler will implicitly add the default argument to the call. However, after Test been converted to a pointer or function reference, default argument information is no longer displayed.

This can be circumvented by creating a functor that really takes zero or one argument, and has such information encoded in its type so that it cannot be destroyed, for example:

 struct Test { void operator()(int) { /* ... */ } void operator()() { operator(int{}); } } test; // ... Foo(test, 0); // ok Foo(test); // ok 
+11
source share

All Articles