是顶级挥发或函数原型限制显著? [英] Is top-level volatile or restrict significant in a function prototype?

查看:105
本文介绍了是顶级挥发或函数原型限制显著?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下原型之间的实际差别?

Is there any practical difference between the following prototypes?

void f(const int *p);

void f(const int *restrict p);

void f(const int *volatile p);

本节C11 6.7.6.3/15(最后一句)说顶级预选赛不考虑确定类型的兼容性的目的,也就是说,它允许对函数的定义会对它的参数不同的顶级预选赛比原型声明了。

The section C11 6.7.6.3/15 (final sentence) says that top-level qualifiers are not considered for the purposes of determining type compatibility, i.e. it is permitted for the function definition to have different top-level qualifiers on its parameters than the prototype declaration had.

但是(不像C ++)不说,他们完全忽略。在常量的情况下这显然是没有实际意义;然而,在案件挥发性限制也许有可能是不同的。

However (unlike C++) it does not say that they are ignored completely. In the case of const this is clearly moot; however in the case of volatile and restrict maybe there could be a difference.

示例:

void f(const int *restrict p);

int main()
{
     int a = 42;
     const int *p = &a;
     f(p);
     return a;
}

请问的presence限制原型让编译器优化出的读 A 返回;

Does the presence of restrict in the prototype allow the compiler to optimize out the read of a for return a; ?

(<一个href=\"http://stackoverflow.com/questions/26346301/does-const-t-restrict-guarantee-the-object-pointed-to-isn-t-modified/\">Related问题)

推荐答案

假设的定义˚F缺乏限制预选赛中,code应该明确定义。 C11(n1570)6.5.2.2(函数调用)P7 [EMPH。煤矿,在C99 TC3相同的措辞(n1256)]

Assuming a definition of f lacking the restrict qualifier, the code should be well-defined. C11 (n1570) 6.5.2.2 (Function calls) p7 [emph. mine, identical wording in C99 TC3 (n1256)]

如果它表示所调用的函数的前pression有一个类型,做包括一个原型,参数被隐式转换,仿佛被分配到类型相应的参数,服用的类型每个参数是其声明的类型的不合格版本

If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type.

功能˚F被调用,不合格的参数(因此,用正确类型的参数),它的所有声明都是兼容的类型(按报价在问题):函数调用是明确的。 (如果没有在做它明确定义。我不认为这是标准的东西。)

The function f is called with unqualified arguments (and thus, with arguments of the correct types), and all its declarations are of compatible type (as per the quote in the question): The function call is well-defined. (If there isn't anything in the standard making it explicitly undefined. I don't think there is.)

这篇关于是顶级挥发或函数原型限制显著?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆