简体   繁体   English

不合格的 sort()——为什么在 std::vector 上使用而不是在 std::array 上编译,哪个编译器是正确的?

[英]Unqualified sort() -- why does it compile when used on std::vector and not on std::array, and which compiler is correct?

When calling std::sort() on a std::array :std::array上调用std::sort()时:

#include <vector>
#include <array>
#include <algorithm>

int main() {
    std::vector<int> foo{4, 1, 2, 3};
    sort(begin(foo), end(foo));

    std::array<int, 4> foo2{4, 1, 2, 3};
    sort(begin(foo2), end(foo2));
}

Both gcc and clang return an error on the sort on the std::array -- clang says gcc 和 clang 都在std::array上的排序上返回错误——clang 说

error: use of undeclared identifier 'sort';错误:使用未声明的标识符“排序”; did you mean 'std::sort'?你的意思是'标准::排序'?

Changing to std::sort(begin(foo2), end(foo2)) fixes the problem.更改为std::sort(begin(foo2), end(foo2))解决问题。

MSVC compiles the code above as written. MSVC 按照编写的方式编译上面的代码。

Why the difference in treatment between std::vector and std::array ;为什么std::vectorstd::array之间的处理不同; and which compiler is correct?哪个编译器是正确的?

This is comes down to the type that begin and end result to and how that works with Argument Dependent Lookup .这归结为beginend结果的类型以及它如何与Argument Dependent Lookup 一起使用

In

sort(begin(foo), end(foo));

you get你得到

sort(std::vector<int>::iterator, std::vector<int>::iterator)

and since std::vector<int>::iterator is a member of std ADL finds sort in std and the call succeeds.并且由于std::vector<int>::iteratorstd ADL 的成员,因此在std找到sort并且调用成功。

With

sort(begin(foo2), end(foo2));

You get你得到

sort(int*, int*)

and because int* is not a member of std , ADL will not look into std and you can't find std::sort .并且因为int*不是std的成员,ADL 不会查看std并且您找不到std::sort

This works in MSVC because这在 MSVC 中有效,因为

sort(begin(foo2), end(foo2));

becomes变成

sort(std::_Array_iterator, std::_Array_iterator)

and since std::_Array_iterator is part of std ADL finds sort .并且由于std::_Array_iteratorstd ADL 的一部分std::_Array_iterator sort

Both compilers are correct with this behavior.两个编译器都对这种行为是正确的。 std::vector and std::array don't have any requirement on what type is used for the iterator except that it satisfies the LegacyRandomAccessIterator requirement and in C++ 17 for std::array that the type also be a LiteralType and in C++20 that it be a ConstexprIterator std::vectorstd::array对迭代器使用的类型没有任何要求,只是它满足LegacyRandomAccessIterator要求,并且在 C++17 中对于std::array类型也是LiteralType和 C+ +20 它是ConstexprIterator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将 std::vector::insert 传递给 std::vector::insert 时,编译器如何推断要调用哪个版本的 std::vector::begin()? - How does the compiler deduce which version of std::vector::begin() to call when passing it to std::vector::insert? 为什么在三元运算符中使用std :: ostream不能编译? - Why does std::ostream not compile when used in ternary operator? 为什么std :: sort()会改变排序的向量? - Why does std::sort() change the sorted vector? 为什么c ++用零来初始化std :: vector,而不是std :: array? - Why does c++ initialise a std::vector with zeros, but not a std::array? 编译器不推导出模板参数(map std :: vector - > std :: vector) - Compiler does not deduce template parameters (map std::vector -> std::vector) 为什么将 std::sort 与自定义比较器一起使用无法编译? - Why does this use of std::sort with a custom comparator not compile? 为什么std :: sort似乎会替换向量中的值 - Why does std::sort appears to alternate the values in the vector 如果没有 noexcept 移动构造函数,为什么带有 std::vector 的代码不能编译,但带有 std::unique_ptr 的代码却可以编译? - Why does code with std::vector not compile but with std::unique_ptr it does, if there is no noexcept move constructor? std :: move of string literal - 哪个编译器正确? - std::move of string literal - which compiler is correct? 当使用std :: vector :: reserve时,std :: sort顺序从最大到最小? - std::sort order is from greatest to least when std::vector::reserve is used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM