gpt4 book ai didi

c++ - C++11 和 C++14 中 `bsearch` 的规范是否有缺陷?

转载 作者:IT老高 更新时间:2023-10-28 14:00:02 26 4
gpt4 key购买 nike

继续我对 this question 的回答, 在 C++11 和 C++14 中:

[C++11, C++14: 25.5/2]: The contents are the same as the Standard C library header <stdlib.h> with the following exceptions:

[C++11, C++14: 25.5/3]: The function signature:

bsearch(const void *, const void *, size_t, size_t,
int (*)(const void *, const void *));

is replaced by the two declarations:

extern "C" void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

extern "C++" void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

both of which have the same behavior as the original declaration.

然而,

[C++11, C++14: 7.5/5]: If two declarations declare functions with the same name and parameter-type-list (8.3.5) to be members of the same namespace or declare objects with the same name to be members of the same namespace and the declarations give the names different language linkages, the program is ill-formed; no diagnostic is required if the declarations appear in different translation units. [..]

这是缺陷吗?

最佳答案

但参数类型列表不一样。一种,compar 是指向具有“C”语言链接的函数的指针,另一种是指向具有“C++”语言链接的函数的指针。

C++11, 7.5 规定:

1 ... Two function types with different language linkages are distinct types even if they are otherwise identical.

4 In a linkage-specification, the specified language linkage applies to the function types of all function declarators, function names with external linkage, and variable names with external linkage declared within the linkage-specification. [ Example:

extern "C" void f1(void(*pf)(int));
// the name f1 and its function type have C language
// linkage; pf is a pointer to a C function

7.5/1 和 7.5/5 之间看似不一致的问题在意识到 1 谈论函数类型而 5 谈论函数名称时得到解决。

关于c++ - C++11 和 C++14 中 `bsearch` 的规范是否有缺陷?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26160962/

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