Index: include/bits/stl_iterator_base_funcs.h =================================================================== --- include/bits/stl_iterator_base_funcs.h (revision 222076) +++ include/bits/stl_iterator_base_funcs.h (working copy) @@ -59,20 +59,26 @@ #ifndef _STL_ITERATOR_BASE_FUNCS_H #define _STL_ITERATOR_BASE_FUNCS_H 1 #pragma GCC system_header #include #include namespace std _GLIBCXX_VISIBILITY(default) { +_GLIBCXX_BEGIN_NAMESPACE_CONTAINER + // Forward declaration for the overloads of __distance. + template struct _List_iterator; + template struct _List_const_iterator; +_GLIBCXX_END_NAMESPACE_CONTAINER + _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline typename iterator_traits<_InputIterator>::difference_type __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) @@ -89,20 +95,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename iterator_traits<_RandomAccessIterator>::difference_type __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) { // concept requirements __glibcxx_function_requires(_RandomAccessIteratorConcept< _RandomAccessIterator>) return __last - __first; } +#if _GLIBCXX_USE_CXX11_ABI + // Forward declaration because of the qualified call in distance. + template + ptrdiff_t + __distance(_GLIBCXX_STD_C::_List_iterator<_Tp>, + _GLIBCXX_STD_C::_List_iterator<_Tp>, + input_iterator_tag); + + template + ptrdiff_t + __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>, + _GLIBCXX_STD_C::_List_const_iterator<_Tp>, + input_iterator_tag); +#endif + /** * @brief A generalization of pointer arithmetic. * @param __first An input iterator. * @param __last An input iterator. * @return The distance between them. * * Returns @c n such that __first + n == __last. This requires * that @p __last must be reachable from @p __first. Note that @c * n may be negative. * Index: include/bits/stl_list.h =================================================================== --- include/bits/stl_list.h (revision 222076) +++ include/bits/stl_list.h (working copy) @@ -1861,13 +1861,52 @@ _GLIBCXX_END_NAMESPACE_CXX11 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { return !(__x < __y); } /// See std::list::swap(). template inline void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y) { __x.swap(__y); } _GLIBCXX_END_NAMESPACE_CONTAINER + +#if _GLIBCXX_USE_CXX11_ABI +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + // Detect when distance is used to compute the size of the whole list. + template + inline ptrdiff_t + __distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first, + _GLIBCXX_STD_C::_List_iterator<_Tp> __last, + input_iterator_tag __tag) + { + typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter; + return std::__distance(_CIter(__first), _CIter(__last), __tag); + } + + template + inline ptrdiff_t + __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first, + _GLIBCXX_STD_C::_List_const_iterator<_Tp> __last, + input_iterator_tag) + { + typedef _GLIBCXX_STD_C::_List_node _Sentinel; + _GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last; + ++__beyond; + bool __whole = __first == __beyond; + if (__builtin_constant_p (__whole) && __whole) + return static_cast(__last._M_node)->_M_data; + + ptrdiff_t __n = 0; + while (__first != __last) + { + ++__first; + ++__n; + } + return __n; + } + +_GLIBCXX_END_NAMESPACE_VERSION +#endif } // namespace std #endif /* _STL_LIST_H */ Index: testsuite/23_containers/list/61347.cc =================================================================== --- testsuite/23_containers/list/61347.cc (revision 0) +++ testsuite/23_containers/list/61347.cc (working copy) @@ -0,0 +1,49 @@ +// { dg-options "-std=gnu++11 -O2 -D_GLIBCXX_USE_CXX11_ABI" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +__attribute__((__noinline__, __noclone__)) +void testm(std::list& l) +{ + bool b = std::distance(l.begin(), l.end()) == l.size(); + VERIFY( __builtin_constant_p(b) ); + VERIFY( b ); +} + +__attribute__((__noinline__, __noclone__)) +void testc(const std::list& l) +{ + bool b = std::distance(l.begin(), l.end()) == l.size(); + VERIFY( __builtin_constant_p(b) ); + VERIFY( b ); +} + +int main() +{ + bool test __attribute__((unused)) = true; + +#if _GLIBCXX_USE_DUAL_ABI + std::list l; + testm(l); + testc(l); +#endif +}