Home > database >  std::array<int> iterator convertible to int* on clang but not MSVC?
std::array<int> iterator convertible to int* on clang but not MSVC?

Time:06-09

So I have some code like

void func( const int* begin, const int* end );

and then want to use std::array<int, X> to have the data stored, and then call the function like so:

std::array<int, 5> data = {1,2,3,4,5};
func( data.begin(), data.end() );

When using clang, the iterator apparently is implicitly convertible to const int* and everything works as expected.

However on MSVC I'm getting a compiler error

C2664 cannot convert argument 1 from `std::_Array_const_iterator<_Ty,5>` to `const int*`

Is there a way to coerce the type conversion that I'm somehow missing? Or will I have to do data.data()[0] or something lame like that?

Changing the function signature is not really an option

CodePudding user response:

The iterator for std::array<int> doesn't necessarily have to be int*, it's just std::array<int>::iterator, which is up to the individual compiler implementation to decide what it should be.

the standard library provides a function to reliably convert iterators into raw pointers:

std::addressof(*iterator)

Although this can also work:

&(*iterator)

  • Related