This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[libstdc++] Use typeof to enable concept checks in algorithms
- From: Phil Edwards <phil at jaj dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Sun, 10 Feb 2002 04:12:07 -0500
- Subject: [libstdc++] Use typeof to enable concept checks in algorithms
Some of the more useful concept checks have been disabled for a while
because they were too specific in what they were checking. I've received a
few reports of "bugs" in the concept checking code because stuff slipped by
that should have been caught by the disabled checks, so getting these working
and re-enabled has been a priority for me. (Never had time until now.)
The sensible thing here is to use __typeof__ to discover the return type
of arbitrary functions, rather than assuming that the return type is the
same as the input iterators' value type. This lets the compiler adapt
itself to cases where these two types are not the same; previously the
checks were mistakenly rejecting such cases.
Tested on i686-linux with no regressions and applied. Suitable for trunk
only; we never did any of the c-c stuff on the 3.0 branch.
2002-02-10 Phil Edwards <pme@gcc.gnu.org>
* include/bits/stl_algo.h (transform (both signatures), generate_n):
Use __typeof__ in concept checks.
Index: include/bits/stl_algo.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_algo.h,v
retrieving revision 1.20
diff -u -3 -p -r1.20 stl_algo.h
--- stl_algo.h 2002/02/10 09:00:41 1.20
+++ stl_algo.h 2002/02/10 09:03:12
@@ -737,11 +737,9 @@ namespace std
{
// concept requirements
__glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
- /* XXX
__glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
- // should be "the type returned by _UnaryOperation"
- typename iterator_traits<_InputIter>::value_type>)
- */
+ // "the type returned by a _UnaryOperation"
+ __typeof__(__unary_op(*__first))>)
for ( ; __first != __last; ++__first, ++__result)
*__result = __unary_op(*__first);
@@ -775,11 +773,9 @@ namespace std
// concept requirements
__glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
__glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
- /* XXX
__glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
- // should be "the type returned by _BinaryOperation"
- typename iterator_traits<_InputIter1>::value_type>)
- */
+ // "the type returned by a _BinaryOperation"
+ __typeof__(__binary_op(*__first1,*__first2))>)
for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
*__result = __binary_op(*__first1, *__first2);
@@ -948,11 +944,10 @@ namespace std
_OutputIter
generate_n(_OutputIter __first, _Size __n, _Generator __gen)
{
- /*
- // XXX concept requirements
+ // concept requirements
__glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
- "the return type of _Generator" ?? >)
- */
+ // "the type returned by a _Generator"
+ __typeof__(gen())>)
for ( ; __n > 0; --__n, ++__first)
*__first = __gen();