是C ++ 11中的`bsearch`的规范, C ++ 14有缺陷? [英] Is the specification of `bsearch` in C++11 & C++14 defective?

查看:145
本文介绍了是C ++ 11中的`bsearch`的规范, C ++ 14有缺陷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续回答这个问题,在C ++ 11和C ++ 14中:


[C ++ 11,C ++ 14:25.5 / 2]:内容与标准C库标题< stdlib.h> 相同,但以下情况除外:



code> [C ++ 11,C ++ 14:25.5 / 3]:函数签名:



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

由两个声明替换:

  externCvoid * bsearch(const void * key,const void * base,
size_t nmemb,size_t size,
int *,const void *));

externC ++void * bsearch(const void *,const void * base,
size_t nmemb,size_t size,
int const void *));

两者都具有与原始声明相同的行为。


但是,


C ++ 14:7.5 / 5]:如果两个声明声明具有相同名称和参数类型列表(8.3.5)的函数成为同一命名空间的成员或声明具有相同名称为同一命名空间的成员,并且声明给予名称不同的语言联系,程序是不成形的;如果声明显示在不同的翻译单元中,则不需要诊断。 [..]


这是一个缺陷吗?

解决方案

但是参数类型列表不一样。在一个实例中, comparator 是指向具有C语言链接的函数的指针,另一个是指向具有C ++语言链接的函数的指针。 p>

C ++ 11,7.5指定:


1 ...两个函数类型用不同的语言
链接是不同的类型,即使它们是相同的。



4在链接规范链接适用于在 linkage-specification中声明的所有函数声明符,具有外部链接的函数名和具有外部
链接的变量名的函数

  externCvoid f1(void(* pf)(int)); 
//名称f1及其函数类型有C语言
// linkage; pf是指向C函数的指针


7.5之间的似乎不一致/ 1和7.5 / 5在实现1个关于功能类型的谈话时解决,而5个地址命名名称。


Following on from my answer to this question, in both C++11 and 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.

However,

[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. [..]

Is this a defect?

解决方案

But the parameter types list are not the same. In one, compar is a pointer to a function with "C" language linkage, in the other one, it's a pointer to a function with "C++" language linkage.

C++11, 7.5 specifies:

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

The seeming inconsistency between 7.5/1 and 7.5/5 is solved when realising that 1 talks about function types, while 5 addresses function names.

这篇关于是C ++ 11中的`bsearch`的规范, C ++ 14有缺陷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆