This is the mail archive of the
libstdc++-prs@sourceware.cygnus.com
mailing list for the libstdc++ project.
libstdc++/10: _Expr<>.sum() in bits/valarray_meta.h doesn't compile
- To: libstdc++-gnats at sourceware dot cygnus dot com
- Subject: libstdc++/10: _Expr<>.sum() in bits/valarray_meta.h doesn't compile
- From: Roman dot Kagan at itep dot ru
- Date: 18 Sep 1999 17:43:29 -0000
- Reply-To: Roman dot Kagan at itep dot ru
- Resent-Cc: libstdc++-prs at sourceware dot cygnus dot com,
- Resent-Reply-To: gnats-admin@sourceware.cygnus.com, Roman.Kagan@itep.ru
- Resent-To: nobody at sourceware dot cygnus dot com
>Number: 10
>Category: libstdc++
>Synopsis: _Expr<>.sum() in bits/valarray_meta.h doesn't compile
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sat Sep 18 10:47:01 PDT 1999
>Closed-Date:
>Last-Modified:
>Originator: Roman Kagan
>Release: 2.90.6 and 2.10.0 (bundled with gcc-2.95.1)
>Organization:
>Environment:
i686 linux-2.2.10 glibc-2.1.1 gcc-2.95.1
>Description:
Template class _Expr in bits/valarray_meta.h has method sum(),
which starts with line
_Tp __s(_Tp());
It is (incorrectly?) interpreted by g++ as a function declaration
rather than an object definition with initialization (BTW so do
other compilers - KCC, IBM xlC, HP aCC). Such behavior is also
mentioned as a known bug of gcc (see http://gcc.gnu.org/bugs.html,
section "Parse errors for "simple" code").
>How-To-Repeat:
#include <valarray>
main()
{
std::valarray<float> a(3);
(a * a).sum();
}
>Fix:
--- libstdc++-2.90.6/bits/valarray_meta.h~ Thu Jun 10 23:05:29 1999
+++ libstdc++-2.90.6/bits/valarray_meta.h Sat Sep 18 20:37:27 1999
@@ -716,14 +716,14 @@
inline size_t
_Expr<_Clos,_Tp>::size () const { return _M_closure.size (); }
- // XXX: replace this with a more robust summation algorithm.
template<class _Clos, typename _Tp>
inline _Tp
_Expr<_Clos,_Tp>::sum () const
{
- _Tp __s(_Tp());
- size_t __n (_M_closure.size ());
- for (size_t __i=0; __i<__n; ++__i) __s += _M_closure[__i];
+ size_t __i (_M_closure.size ());
+ if(!__i) return _Tp();
+ _Tp __s(_M_closure[--__i]);
+ while(__i) __s += _M_closure[--__i];
return __s;
}
It also reduces the number of temporaries and constructor calls,
which might be worth doing to the rest of the valarray files.
>Release-Note:
>Audit-Trail:
>Unformatted: