Function pointers with default parameters in C++

后端 未结 2 442
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 18:20

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         


        
相关标签:
2条回答
  • 2021-01-03 18:27

    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]

    0 讨论(0)
  • 2021-01-03 18:45

    Default argument cannot be provided for pointers to functions.

    0 讨论(0)
提交回复
热议问题