加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11标准中该声明的含义是什么?

发布时间:2020-12-16 03:43:11 所属栏目:百科 来源:网络整理
导读:从C 11标准第5.2.2 / 1段中提取的句子中粗体字符是什么意思? There are two kinds of function call: ordinary function call and member function (9.3) call. A function call is a postfix expression followed by parentheses containing a possibly em
从C 11标准第5.2.2 / 1段中提取的句子中粗体字符是什么意思?

There are two kinds of function call: ordinary function call and member function (9.3) call. A function call is a postfix expression followed by parentheses containing a possibly empty,comma-separated list of expressions which constitute the arguments to the function. For an ordinary function call,the postfix expression shall be either an lvalue that refers to a function (in which case the function-to-pointer standard conversion (4.3) is suppressed on the postfix expression),or it shall have pointer to function type.

编辑

基本上我要问的是:当标准说“(在这种情况下,函数到指针标准转换在后缀表达式上被抑制)”这是否意味着这种抑制是好的或者它将在以后被撤销(例如,在函数重载之后)?

EDIT1

在我看来,上面的“抑制”一词具有误导性,因为它给人的印象是,从函数名到函数指针的转换可能永远不会被编译器完成.我相信情况并非如此.在函数重载过程完成后,此转换将始终发生,因为只有在此时,编译器才能确切地知道要调用的函数.程序初始化函数指针时不是这种情况.在这种情况下,编译器知道要调用的函数,因此不会出现重载,如下例所示.

#include <iostream>
int foo(int i) { std::cout << "int" << 'n'; return i * i; }
double foo(double d) { std::cout << "double" << 'n'; return d * d; }

int main()
{
    int(*pf)(int) = foo;                //  no overloading here!
    std::cout << pf(10.) << 'n';       //  calls foo(int)
    std::cout << foo(10.) << 'n';      //  call foo(double) after function overloading. Therefore,only after function
                                        //  overloading is finished,the function name foo() is converted into a function-pointer.
}

代码打印:

int
100
double
100

解决方法

For an ordinary function call,or it shall have pointer to function type.

因此,“后缀表达式”在5.2 / 1中定义…它是一个语法元素,并不一定意味着“x”类型的后缀……看一下列表本身.基本上,关键是有一个表达式:

>“应该是引用函数的左值(在这种情况下,后缀表达式上的函数到指针标准转换(4.3)被抑制)”

这意味着表达式实际上是函数的标识符,或类似(x?fn1:fn2).它不需要转换为指针 – 编译器已经知道如何调用函数.

(值得注意的是它必须不进行转换的原因是运算符优先级 – 特别是后缀和 –,()用于函数调用,[],.和 – >都具有相同的优先级和从左到右的关联性,因此例如[1] f(1,2,3)被视为[1](f(1,3)),因为它是唯一的解析匹配,而如果发生指针衰减,那么从左到右的相关性将其视为([1] f)(1,3).)

>“或它应具有指向功能类型的指针.”

所以,你也可以提供一个函数指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读