Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c++ - Is the specification of `bsearch` in C++11 & C++14 defective?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...