Destructor
Destructor

Reputation: 14438

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

Consider following program:

#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';
}

This program compiles fine on g++ & clang++.

See live demo here ( g++ 5.4.0 )

See live demo here ( clang++ 3.8.0 )

But it gives horrible compiler error on Microsoft Visual C++ compiler.

See live demo here ( Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 )

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
        ]

So the question is which compiler is right here according to C++ standard ? Is this bug in VC++ compiler ?

Upvotes: 2

Views: 305

Answers (1)

songyuanyao
songyuanyao

Reputation: 172994

Is this bug in VC++ compiler ?

No.

You're assigning and comparing the iterators got from std::begin and std::end on raw array (i.e. result, std::find(std::begin(arr),std::end(arr),num)) and std::array (i.e. std::find_if_not(std::begin(x),std::end(x),divisible_by_five) and std::end(x)), you might be supposing that the types of them are the same.

For raw arrays it'll be T*, i.e. int* for this case, this is guaranteed. The problem is that the standard doesn't specify the exact type of std::array::iterator, it just says it must satisfy the requirements of RandomAccessIterator. Gcc and Clang choose the int* as its type, this is fine because the raw pointer satisfy the requirements. VC implements it as a customized class, this is fine too so long as the type satisfies the requirements. And note that that type doesn't have to be able to convert to int*; the standard doesn't require that at all.

So even your code works with Gcc and Clang, it's not guaranteed.

Upvotes: 11

Related Questions