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

时间:2018-03-09 09:28:08

标签: c++ visual-c++ g++ language-lawyer clang++

考虑以下计划:

#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 ++&amp;铛++

参见现场演示here(g ++ 5.4.0)

参见现场演示here(clang ++ 3.8.0)

但它在Microsoft Visual C ++编译器上提供了可怕的编译器错误。

查看实时演示here(适用于x64的Microsoft(R)C / C ++优化编译器版本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 ++编译器中的错误吗?

1 个答案:

答案 0 :(得分:11)

  

这是VC ++编译器中的错误吗?

没有。

您正在分配和比较原始数组上std::beginstd::end获得的迭代器(即resultstd::find(std::begin(arr),std::end(arr),num))和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将其实现为自定义类,只要类型满足要求,这也很好。请注意,该类型无法转换为int*;标准根本不需要这样做。

因此,即使您的代码与Gcc和Clang一起使用,也无法保证。