gpt4 book ai didi

这个函数可以兼容两个版本的 POSIX 吗?

转载 作者:太空狗 更新时间:2023-10-29 15:13:44 25 4
gpt4 key购买 nike

这是 strspn 的概要:

#include <string.h>

size_t strspn(const char *s1, const char *s2);

这是 POSIX.1-2001 的描述和返回值:

The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.

The strspn() function shall return the length of s1; no return value is reserved to indicate an error.

这(几乎)与 POSIX.1-2017 相同:

The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.

The strspn() function shall return the computed length; no return value is reserved to indicate an error.

strspn 的实现是否有可能同时符合 POSIX.1-2001 和 POSIX.1-2017?怎么办?

最佳答案

这是 POSIX.1-2001 中的错误。

作为the POSIX description of strspn says :

The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.

而 C 标准(ISO 9899:1999, 7.21.5.6 strspn 函数)明确表示:

The strspn function returns the length of the segment.

较新版本的 POSIX 修正了措辞,使其与 C 标准表达的意思相同,这也是一直以来的意图。 (它显然在 2006 年被注意到并发生了变化;参见 https://www.opengroup.org/austin/docs/austin_330.txtcurrent version of strspn in POSIX 在“更改历史记录”部分中将此(相当隐晦地)称为“SD5-XSH-ERN-182 已应用” .)

正如 POSIX 所说,它“遵从 ISO C 标准”,我认为当存在冲突时,兼容的实现必须遵循 C 标准,例如在这种情况下。

关于这个函数可以兼容两个版本的 POSIX 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com