编译器确定参数是否给出或省略

3 c++ function default-arguments

如何确定/检查在编译时是否给出或省略了函数参数?

bool a_function(char* b, size_t len=0) {

      // no run time check such as if (len ......
      // just compile time check

      // ...
}
Run Code Online (Sandbox Code Playgroud)

如何实现?

son*_*yao 6

不,没有办法知道(即使在运行时)为函数中具有默认参数的参数指定了参数。

您可以应用重载,例如

bool a_function(char* b, size_t len) {

    // len is specified
    // do something...
}
bool a_function(char* b) {

    // len is not specified
    // do something else...
    // or call a_function with len=0 (the default value) if satisfying the requirement
}
Run Code Online (Sandbox Code Playgroud)