This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: _Expr<>.sum() in bits/valarray_meta.h doesn't compile



[...]

| 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). 

GCC is correct on this point.  I should not have used
direct-initialization syntaxe in that contexte because it looks like
function declaration and the compiler has to interpret it as a such.

| ...  Such behavior is also
| mentioned as a known bug of gcc (see http://gcc.gnu.org/bugs.html,
| section "Parse errors for "simple" code").

Well, that bug is really a bug ine the *compiler*.  The one you're
reporting is a bug in my *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.


Thanks.  I'll use copy-initilization synatxe now that the compiler
can optimize away the extra temporary.  It makes the code easier to read. 

Bst,

-- Gaby


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]