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

Des*_*tor 2 c++ g++ visual-c++ 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';
}
Run Code Online (Sandbox Code Playgroud)

这个程序在g ++和clang ++上编译得很好.

在这里查看现场演示(g ++ 5.4.0)

在这里查看现场演示(clang ++ 3.8.0)

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

请参阅此处的实时演示(适用于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
        ]
Run Code Online (Sandbox Code Playgroud)

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

son*_*yao 11

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

没有.

你分配和比较迭代器从得到的std ::开始的std ::结束原始阵列(即上result,std::find(std::begin(arr),std::end(arr),num))和性病::阵列(即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一起使用,也无法保证.

  • OP可能没有捕获`result`从`std :: begin(int(&)[5])`(又名`int*`)得到一个类型,然后被赋值/与返回值进行比较`std :: begin(std :: array <int,4>&)`(标准中一个未指定的随机访问连续迭代器,在clang中是`int*`但在MSVC中没有).您可能希望对此更加明确. (2认同)