This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: possible bug with the stl inner_product() function
- To: "Graham M. Seed" <G dot M dot Seed at hw dot ac dot uk>
- Subject: Re: possible bug with the stl inner_product() function
- From: Ross Smith <ross dot s at ihug dot co dot nz>
- Date: Wed, 01 Nov 2000 08:41:54 +1300
- CC: gcc-bugs at gcc dot gnu dot org
- Organization: Ihug
- References: <SIMEON.10010311611.D@grahams-comp.pp.hw.ac.uk>
"Graham M. Seed" wrote:
>
> Dear Sir/Madam
>
> The following program illustrates uses of the
> inner_product() function of the c++ STL. I'm using version
> 2.91.66 gcc/g++ compiler running at Red Hat Linux 6.2.
>
> The default operators for the inner_product() function are
> *,+ which gives a final answer of 70 as expected. However,
> when we specify the contrary case of +,* operators then I'd
> expect the result to be 5760 but it is still 70??
>
> If this isn't a bug then it certainly doesn't agree with
> the mathematical interpretation of the inner product.
>
> //...
> void main ()
> {
> int array1[] = { 1, 2, 3, 4 };
> int array2[] = { 5, 6, 7, 8 };
>
> ostream_iterator<int> out (cout, " ") ;
> cout << "array1: " ;
> copy (array1, array1+4, out) ;
> cout << endl ;
> cout << "array2: " ;
> copy (array2, array2+4, out) ;
> cout << endl ;
>
> int ip1 = inner_product (array1, array1+4, array2, 0) ;
> cout << "ip1: " << ip1 << endl ; // O/P 70 as expected
>
> int ip2 = inner_product (array1, array1+4, array2, 0, plus<int>(), multiplies<int>()) ;
> cout << "ip2: " << ip2 << endl ; // O/P still 70 when
> should be 5760?
> }
This is not a GCC bug. Your code contains three errors (aside from any
that may be lurking in the ellipsis at the top; any kind of bug report
or request for help should contain a complete program):
(1) void main() invokes undefined behaviour.
(2) You have the fifth and sixth arguments of inner_product reversed.
Your second call uses the same arguments as the default arguments used
in the first call. For reversed operations, you want (..., multiplies,
plus). (See section 26.4.2 of the C++ standard.)
(3) The expected result with the reversed operations is 0, not 5760.
--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"C++ is to programming as sex is to reproduction. Better ways might
technically exist but they're not nearly as much fun." -- Nikolai Irgens