用默认参数解引用函数-C ++ 14 vs C ++ 11 [英] Dereferencing a function with default arguments - C++14 vs C++11
问题描述
以下代码无法在带有选项-std=c++1y
的g ++版本5.4.0中进行编译:
Following code can't be compiled with g++ version 5.4.0 with option -std=c++1y
:
void f(int=0) ;
int main() {
f(); // ok
(*f)(2);// ok
(*f)();// ok c++11; error with c++14: too few arguments to function
return 0;
}
该函数声明为具有默认参数,那么这里出了什么问题? 谢谢你的帮助.
The function declared to have default argument, so what is wrong here? thanks for help.
为什么g++ -c -std=c++11
可以编译?
推荐答案
接受(*f)()
有效是GCC错误.该标准的字母表示使用带有一元*
的函数名称应导致该函数名称衰减为指针.然后应该取消指针的引用,以获取调用表达式的函数地址.
Accepting (*f)()
as valid is a GCC bug. The letter of the standard indicates that using a function name with unary *
should cause the function name to decay into a pointer. The pointer should then be dereferenced to obtain the functions address for the call expression.
但是,海湾合作委员会似乎很聪明,并且忽略了上述行为.它简单地将(*f)
视为f
.调用f
可以使用默认参数完成.
But GCC seems clever, and omits the above behavior. It treats (*f)
simply as f
. And calling f
can be done with default arguments.
但是,可以强迫GCC进行衰减.应用于函数名称的一元+
会强制将其衰减为指针.所以以下两个:
However, one can force GCC to preform the decay. Unary +
applied on the function name will decay it to a pointer forcefully. So the following two:
(+f)();
(*+f)();
在两个 GCC 7.2 的任何一个标准修订版中,导致GCC发出error: too few arguments to function
.和 GCC 6.3 .
Cause GCC to emit error: too few arguments to function
on either standard revision, in both GCC 7.2 and GCC 6.3.
这篇关于用默认参数解引用函数-C ++ 14 vs C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!