Function pointers with default parameters in C++

2019-02-11 10:47发布

How does C++ handle function pointers in relation to functions with defaulted parameters?

If I have:

void foo(int i, float f = 0.0f);
void bar(int i, float f);


void (*func_ptr1)(int);
void (*func_ptr2)(int, float);
void (*func_ptr3)(int, float = 10.0f);

Which function pointers can I use in relation to which function?

2条回答
淡お忘
2楼-- · 2019-02-11 10:58

Default argument cannot be provided for pointers to functions.

查看更多
叛逆
3楼-- · 2019-02-11 11:20

Both foo() and bar() can only be assigned to func_ptr2.

§8.3.6/2:

A default argument is not part of the type of a function. [Example:

int f(int = 0);

void h() {
    int j = f(1);
    int k = f(); // OK, means f(0)
}

int (*p1)(int) = &f; 
int (*p2)() = &f; // error: type mismatch

--end example]

查看更多
登录 后发表回答