clang++ 和 VC++ 上原始数组和 std::array 的不同迭代器行为

Different iterator behavior of raw array and std::array on clang++ and VC++

考虑以下程序:

#include <iostream>
#include <algorithm>
#include <array>
bool greater_than_seven(int i) {
    return i > 5;
}
bool divisible_by_five(int x) {
    return ((x%5)==0);
}
int main() {
    int arr[]{3,6,9,12,15};
    std::cout<<"Enter a number you want to search: ";
    int num;
    std::cin>>num;
    auto result(std::find(std::begin(arr),std::end(arr),num));
    if(result != std::end(arr))
        std::cout<<"arr contains: "<<num<<'\n';
    else
        std::cout<<"arr doesn't contain: "<<num<<'\n';

    for(result=std::find_if(std::begin(arr),std::end(arr),greater_than_seven);result!=std::end(arr);++result)   
        std::cout<<*result<<' ';
    std::cout<<'\n';    
    std::array<int,4> x{33,66,99,55};
    for(result=std::find_if_not(std::begin(x),std::end(x),divisible_by_five);result!=std::end(x);++result)
        std::cout<<*result<<'\n';
}

此程序在 g++ 和 clang++ 上编译良好。

查看现场演示 here ( g++ 5.4.0 )

查看现场演示 here ( clang++ 3.8.0 )

但是它在 Microsoft Visual C++ 编译器上给出了可怕的编译器错误。

查看现场演示 here(Microsoft (R) C/C++ 针对 x64 优化编译器版本 19.00.23506)

Error(s):
source_file.cpp(27): error C2440: '=': cannot convert from 'std::_Array_iterator<_Ty,4>' to 'int *'
        with
        [
            _Ty=int
        ]
source_file.cpp(27): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
source_file.cpp(27): error C2679: binary '!=': no operator found which takes a right-hand operand of type 'std::_Array_iterator<_Ty,4>' (or there is no acceptable conversion)
        with
        [
            _Ty=int
        ]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(343): note: could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(348): note: or       'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(353): note: or       'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(388): note: or       'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(395): note: or       'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(402): note: or       'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(409): note: or       'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
source_file.cpp(27): note: while trying to match the argument list '(int *, std::_Array_iterator<_Ty,4>)'
        with
        [
            _Ty=int
        ]

所以问题是根据 C++ 标准,哪个编译器就在这里?这是 VC++ 编译器中的错误吗?

Is this bug in VC++ compiler ?

没有。

您正在分配和比较从 std::begin and std::end on raw array (i.e. result, std::find(std::begin(arr),std::end(arr),num)) and std::array 获得的迭代器(即 std::find_if_not(std::begin(x),std::end(x),divisible_by_five)std::end(x)),您可能假设它们的类型相同。

对于原始数组,它将是 T*,即对于这种情况,int*,这是有保证的。问题是标准没有指定std::array::iterator的确切类型,它只是说它必须满足RandomAccessIterator的要求。 Gcc 和 Clang 选择 int* 作为它的类型,这很好,因为原始指针满足要求。 VC实现为自定义的class,只要类型满足要求也可以。请注意,该类型不必能够转换为 int*;标准根本不需要。

因此,即使您的代码适用于 Gcc 和 Clang,也不能保证。