是否可以将函数声明为 pure
或 const
(使用 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/