代码之家  ›  专栏  ›  技术社区  ›  Destructor

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

  •  2
  • Destructor  · 技术社区  · 7 年前

    考虑以下计划:

    #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++&上编译良好;叮当声++。

    查看实时演示 here (g++5.4.0)

    查看实时演示 here (叮当声++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 回复  |  直到 7 年前
        1
  •  11
  •   songyuanyao    7 年前

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

    您正在分配和比较从 std::begin std::end 在原始阵列上(即。 result ,则, std::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选择 内景* 作为其类型,这很好,因为原始指针满足要求。VC将其实现为一个定制类,只要类型满足要求,就可以了。请注意,该类型不必能够转换为 内景* ;标准根本没有要求。

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