c - 在 C 和 GCC 中使用带有返回参数的 pure/const 属性?

标签 c gcc constants

是否可以将函数声明为 pureconst(使用 GCC 属性),

带有返回参数? (其中参数用于返回值)。

例如:

void mid_v3_v3v3(float r_out[3], v0[3], v1[3])
{
    r_out[0] = (v0[0] + v1[0]) * 0.5f;
    r_out[1] = (v0[1] + v1[1]) * 0.5f;
    r_out[2] = (v0[2] + v1[2]) * 0.5f;
}

除了r_out之外,此函数是const,是否有某种方法可以将参数标记为返回值,但否则将该函数视为常量

参见: https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Attributes.html

最佳答案

您自己的链接部分回答了这个问题。关于常量:

Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.

Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void.

pure 的文档实际上是错误并且自相矛盾;它说该函数只能访问其参数和/或全局变量,但随后给出了 strlen (访问其参数所指向的成员)作为示例。

无论如何,对于您的“返回参数”用法,我认为这两种方法都不可行。但是,您可以做的是使用返回包含结果数组的结构的函数;那么应该应用 pure 属性(假设 pure 的示例是正确的,而不是文本)。

关于c - 在 C 和 GCC 中使用带有返回参数的 pure/const 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23976790/

相关文章:

c# - 几个 C# 命名约定问题

c++ - 线性同余发生器 : How important is setting seed?

c - 读写动态分配的内存

c - 我的程序无限循环,我真的不知道为什么

c++ - gcc 可以使用旧的第三方库编译 C++17 代码吗?

c - 如何在 C 中的结构中初始化 const(使用 malloc)

c++ - 为什么在具有多个参数的重载 operator+ 中通过 const 引用传递

C指针,我做错了什么?

gcc - 删除GCC ABI变更记录

gcc - 如何使用 GCC 获得更好的矢量化?