Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with default parameter as template type

I am trying to use a function with a default argument as a function pointer template parameter:

template <void (*F)()>
class A {};

void foo1(int a = 0) {}
void foo2() {}

int main() 
{
    //A<foo1> a1;   <-- doesn't work
    A<foo2> a2;
}

The compiler error is:

main.cpp:7:7: error: could not convert template argument ‘foo1’ to ‘void (*)()’

Is there specific syntax for this to work? Or a specific language limitation? Otherwise, the alternative is to have two separate functions instead of a default parameter:

void foo1(int a) {}
void foo1() { foo1(0); }

Update I understand that the signatures are different, but I'm wondering if there is a way to make this work conveniently without needing to modify all the functions with default parameters?

like image 461
JaredC Avatar asked Dec 26 '22 14:12

JaredC


1 Answers

The signature of foo1 is void(int), not void(). This is why it isn't convertible to void(*)().

You are confusing default arguments with overloading.

like image 102
sehe Avatar answered Jan 08 '23 10:01

sehe