From a057a4f13b2599c7fa19bf08b9617b6b3f67d2d7 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 24 Jun 2008 18:02:36 +0000 Subject: [PATCH] stl_algo.h (remove_if): Cast __pred result to bool. 2008-06-24 Paolo Carlini * include/bits/stl_algo.h (remove_if): Cast __pred result to bool. (copy_if): Add, per N2666. * testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/ 2.cc: New. * testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/ pod.cc: Likewise. * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. From-SVN: r137080 --- libstdc++-v3/ChangeLog | 10 ++++ libstdc++-v3/include/bits/stl_algo.h | 43 ++++++++++++++++- .../requirements/explicit_instantiation/2.cc | 47 +++++++++++++++++++ .../explicit_instantiation/pod.cc | 46 ++++++++++++++++++ .../headers/algorithm/synopsis.cc | 14 +++++- 5 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/2.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/pod.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 18af91c45e1..ccb1e1d53f3 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2008-06-24 Paolo Carlini + + * include/bits/stl_algo.h (remove_if): Cast __pred result to bool. + (copy_if): Add, per N2666. + * testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/ + 2.cc: New. + * testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/ + pod.cc: Likewise. + * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. + 2008-06-24 Paolo Carlini Chalathip Thumkanon diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 549c0cac7e7..6b1e8a52bd0 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -712,7 +712,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) * @return An iterator designating the end of the resulting sequence. * * Copies each element in the range @p [first,last) for which - * @p pred returns true to the range beginning at @p result. + * @p pred returns false to the range beginning at @p result. * * remove_copy_if() is stable, so the relative order of elements that are * copied is unchanged. @@ -740,6 +740,45 @@ _GLIBCXX_BEGIN_NAMESPACE(std) return __result; } +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + /** + * @brief Copy the elements of a sequence for which a predicate is true. + * @param first An input iterator. + * @param last An input iterator. + * @param result An output iterator. + * @param pred A predicate. + * @return An iterator designating the end of the resulting sequence. + * + * Copies each element in the range @p [first,last) for which + * @p pred returns true to the range beginning at @p result. + * + * copy_if() is stable, so the relative order of elements that are + * copied is unchanged. + */ + template + _OutputIterator + copy_if(_InputIterator __first, _InputIterator __last, + _OutputIterator __result, _Predicate __pred) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, + typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, + typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_requires_valid_range(__first, __last); + + for (; __first != __last; ++__first) + if (__pred(*__first)) + { + *__result = *__first; + ++__result; + } + return __result; + } +#endif + /** * @brief Remove elements from a sequence. * @param first An input iterator. @@ -816,7 +855,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) _ForwardIterator __result = __first; ++__first; for(; __first != __last; ++__first) - if(!__pred(*__first)) + if(!bool(__pred(*__first))) { *__result = _GLIBCXX_MOVE(*__first); ++__result; diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/2.cc b/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/2.cc new file mode 100644 index 00000000000..b71050628d8 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/2.cc @@ -0,0 +1,47 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-24 Paolo Carlini + +// Copyright (C) 2008 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include +#include + +namespace std +{ + using __gnu_test::NonDefaultConstructible; + + typedef NonDefaultConstructible value_type; + typedef value_type* iterator_type; + typedef std::pointer_to_unary_function predicate_type; + + template iterator_type copy_if(iterator_type, iterator_type, + iterator_type, predicate_type); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/pod.cc b/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/pod.cc new file mode 100644 index 00000000000..7433367d733 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_if/requirements/explicit_instantiation/pod.cc @@ -0,0 +1,46 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-24 Paolo Carlini + +// Copyright (C) 2008 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include + +namespace std +{ + using __gnu_test::pod_int; + + typedef pod_int value_type; + typedef value_type* iterator_type; + typedef std::pointer_to_unary_function predicate_type; + + template iterator_type copy_if(iterator_type, iterator_type, + iterator_type, predicate_type); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc index 4939ebf0bd1..87487b3d8e6 100644 --- a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc +++ b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc @@ -1,6 +1,6 @@ // { dg-do compile } -// Copyright (C) 2007 Free Software Foundation, Inc. +// Copyright (C) 2007, 2008 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 @@ -115,6 +115,12 @@ namespace std void swap(_Tp&, _Tp& b); +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + void + swap(_Tp (&)[_Nm], _Tp (&)[_Nm]); +#endif + template _FIter2 swap_ranges(_FIter1 first1, _FIter1, _FIter2); @@ -180,6 +186,12 @@ namespace std _OIter remove_copy_if(_IIter, _IIter, _OIter, _Predicate); +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + _OIter + copy_if(_IIter, _IIter, _OIter, _Predicate); +#endif + template _FIter unique(_FIter, _FIter); -- 2.30.2