Saruman Saruman - 22 days ago 6
C++ Question

C++ - Can you assume type* = iterator<type>?

I think it is easier to just show this in code:

#include <iostream>
#include <array>

struct Test
{};

int main()
{
std::array<Test, 1> arr {};
Test* t = arr.begin();
}


arr.begin() returns a iterator but as you can see I can refer to it with Test*. This seems to be possible due to implicit cast, can I expect this to work across other compilers as well according to the standard?

Also is it the operator TYPE that does the implicit cast, ex:

operator T*()
{
return &(*this->CONTAINER)[index];
}

template <typename U>
operator U() = delete;


or something else?

Thanks in advance

Answer

As seen in The C++ standard draft an iterator is implementation-defined:

using iterator = implementation-defined; // see [container.requirements]

[container.requirements] says that iterator should be at least:

Any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.

Neither requirements of a forward iterator nor the requirements of the InputIterator (ForwardIterator needs to satisfy requirements for InputIterator) nor the requirements of an Iterator (InputIterator needs to satisfy requirements for Iterator) state that they should have this conversion operator and thus this is very compiler specific and not standard.