[libc++] Comparing vector<T*>::iterator to vector<T*>::const_iterator&

Hi,

The following program cannot be compiled with libc++

#include
#include <unordered_map>

typedef std::vector<int*> KernelVector;
typedef std::unordered_map<int*,KernelVector> KernelVectorMap;

int main()
{
KernelVectorMap::const_iterator kernel;
KernelVector::const_iterator subkernel = kernel->second.begin();

std::distance( kernel->second.begin(), subkernel );

return 0;
}

clang++ -std=c++0x -stdlib=libc++ ./const_iterator.cpp
./const_iterator.cpp:12:3: error: no matching function for call to ‘distance’
std::distance( kernel->second.begin(), subkernel );
^~~~~~~~~~~~~
/usr/include/c++/v1/iterator:478:1: note: candidate template ignored: deduced conflicting types for parameter
‘_InputIter’ (‘std::__1::__wrap_iter<int **>’ vs. ‘std::__1::__wrap_iter<int *const *>’)
distance(_InputIter __first, _InputIter __last)
^
1 error generated.

or

g++ -std=c++0x -I /usr/include/c++/v1/ ./const_iterator.cpp
./const_iterator.cpp: In function ‘int main()’:
./const_iterator.cpp:12:52: error: no matching function for call to ‘distance(std::__1::vector<int*>::iterator, std::__1::vector<int*>::const_iterator&)’
./const_iterator.cpp:12:52: note: candidate is:
/usr/include/c++/v1/iterator:479:47: note: template typename std::__1::iterator_traits::difference_type std::__1::distance(_InputIter, _InputIter)

Please note that there’s no error produced when libstdc++ used.
My expectation was that libc++ silently convert iterator to const_iterator if necessary, but I guess I’m wrong.

Any comments will be greatly appreciated.

Thanks,

Ryuta

My expectation was that libc++ silently convert iterator to const_iterator
if necessary, but I guess I'm wrong.
Any comments will be greatly appreciated.

Looks like you're right to expect that, by my reading: 23.2.1\4, Table
96, row 3:

Expression: X::iterator
...
Assertion/note pre-/post-condition: .... convertible to X::const_iterator.

Hi David,

Thanks for the info. I filed the bug report.

Regards,

Ryuta

Thanks! Fix committed revision 135854.

It was a problem with the pointer type of unordered_map::const_iterator. It was value_type* and should have been const value_type*.

Howard

Hi Howard,

I verified the fix worked. Thanks!

Regards,

Ryuta