小编rao*_*lla的帖子

在默认参数结束之前,C++编译器不会警告缺少参数

我用3个参数声明了一个C++函数声明,其中两个默认值是这样的.

void func(int const n, bool const flag=true, int *array=NULL) {
  /* print contents of array */
}
Run Code Online (Sandbox Code Playgroud)

当我错误地调用函数时,省略第二个参数但包括第三个参数,就像这样

int array[5]={1,2,3,4,5};
func(5,array);
Run Code Online (Sandbox Code Playgroud)

gcc和intel编译器(Ubuntu 14.04 LTS上的默认编译器)都没有抱怨指定了最后一个参数而没有指定倒数第二个参数.代码运行但是为数组发送了NULL(我希望代码失败).

我的问题是为什么编译器没有抱怨它找不到匹配的函数,因为我的调用的签名应该出现为

funct(int const, int *)
Run Code Online (Sandbox Code Playgroud)

我可以在编译期间打开哪些选项来触发有关此错误使用的警告?

c++ compiler-warnings default-arguments

5
推荐指数
1
解决办法
256
查看次数

标签 统计

c++ ×1

compiler-warnings ×1

default-arguments ×1