我对restrict
有一个大概的了解,但希望能澄清一些细节。我有一个函数,从一个缓冲区读取以空字符结尾的字符串,并在另一个缓冲区中写出URL编码的版本。该函数具有以下签名(当前没有restrict
):
I have a general understanding of restrict
but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature (currently without restrict
):
char const *StringUrlEncode(char const *unencoded,
char *encoded,
char *encodedEnd);
unencoded
是我的以空字符结尾的源字符串。目标缓冲区由encoded
和encodedEnd
表示,其中encoded
指向缓冲区中第一个char
,而encodedEnd
指向缓冲区之后的第一个字符,即该函数将写入char
直到但不包括encodedEnd
所指向的位置--如果您熟悉C++ STL约定,这就是您基本的begin
/end
迭代器对。
如果我在此函数中添加restrict
,是否应仅应用于前两个参数:
char const *StringUrlEncode(char const *restrict unencoded,
char *restrict encoded,
char *encodedEnd);
还是我没有理解添加到所有三个参数中有什么好处吗?
我可以看出将输入和输出缓冲区设置为restrict
有助于编译器知道它们不重叠。但是由于最后一个参数encodedEnd
仅用于标记输出缓冲区的结尾,我想restrict
在这里并没有帮助编译器(虽然我认为这不会有害,除了在函数声明中增加不必要的噪音)。