C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

inquiry about correct types in std::partial_sum

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jonathan Higa
Guest





PostPosted: Mon Aug 08, 2005 7:47 am    Post subject: inquiry about correct types in std::partial_sum Reply with quote



Consider the following translation unit:

#include <functional>
#include <vector>
class Integer {
public:
Integer();
Integer(int);
friend Integer operator *(Integer const &, Integer const &);
};
std::vector<Integer> partial_product(std::vector<int> const &x)
{
std::vector<Integer> y(x.size());
std::partial_sum(x.begin(), x.end(), y.begin(),
std::multiplies<Integer>());
return y;
}

According to Clause 26.4.3, the computations performed by partial_sum
should use std::multiples<Integer>. Nevertheless, at least two
compilers I've recently used will complain that no conversion exists
between Integer and int.

Is my example correct?

Curiously,
Jonathan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
chris jefferson
Guest





PostPosted: Mon Aug 08, 2005 4:42 pm    Post subject: Re: inquiry about correct types in std::partial_sum Reply with quote



Jonathan Higa wrote:
Quote:
Consider the following translation unit:

#include <functional
partial_sum is in
#include <vector
class Integer {
public:
Integer();
Integer(int);
friend Integer operator *(Integer const &, Integer const &);
};
std::vector {
std::vector<Integer> y(x.size());
std::partial_sum(x.begin(), x.end(), y.begin(),
std::multiplies<Integer>());
return y;
}

According to Clause 26.4.3, the computations performed by partial_sum
should use std::multiples<Integer>. Nevertheless, at least two
compilers I've recently used will complain that no conversion exists
between Integer and int.

While there is a problem, it's not the one you think it is. The problem
is that after two elements of the first array have been added together,
the obvious implement then needs to store the added together values, and
then loop around.

In all implementations I know of (perhaps simply as they derive from a
common source), this is the value_type of the first iterator. So what
happens is the first two elements are added together and assigned to the
first value of the output range, and then the result of the addition is
assigned to an int. This spews.

I've seen a few discussions of this and similar issues. It's hard to
know exactly what the standard requires. One option would be to instead
use the value_type of the output range, but that might break some other
programs. The best idea would be to have some kind of type_of operator,
and then store a+b in typeof(a+b), which I would imagine would cause
least arguments.

For now I expect the behaviour to stay much as it is I'm afraid. If your
example is correct, and how if at all it should be fixed, will have to
be decided by the c++ committee.

Chris

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Mon Aug 08, 2005 7:52 pm    Post subject: Re: inquiry about correct types in std::partial_sum Reply with quote



In article <dd7v1d$oa1$1 (AT) pump1 (DOT) york.ac.uk>, chris jefferson
<caj (AT) cs (DOT) york.ac.uk> wrote:

Quote:
Jonathan Higa wrote:
Consider the following translation unit:

#include <functional
partial_sum is in
#include <vector
class Integer {
public:
Integer();
Integer(int);
friend Integer operator *(Integer const &, Integer const &);
};
std::vector {
std::vector<Integer> y(x.size());
std::partial_sum(x.begin(), x.end(), y.begin(),
std::multiplies<Integer>());
return y;
}

According to Clause 26.4.3, the computations performed by partial_sum
should use std::multiples<Integer>. Nevertheless, at least two
compilers I've recently used will complain that no conversion exists
between Integer and int.

While there is a problem, it's not the one you think it is. The problem
is that after two elements of the first array have been added together,
the obvious implement then needs to store the added together values, and
then loop around.

In all implementations I know of (perhaps simply as they derive from a
common source), this is the value_type of the first iterator. So what
happens is the first two elements are added together and assigned to the
first value of the output range, and then the result of the addition is
assigned to an int. This spews.

But the result iterator_category is output_iterator_tag and output

iterators do not need a value_type being the result stored by the
output iterator. [the standard output iterators typedef void
value_type, via std::iterator<....>]
The temp output result must be stored as output iterators can only
move forward, so what else can it store the temp in except the
input_iterator::value_type?

Solution is to provide an operator int() or use
boost::transform_iterator to convert the sequence of ints into a
sequence of Integers, one at a time as needed by partial_sum.

template <typename Result,typename Arg>
struct convert
{
Result operator () (const Arg &x) const {return Result(x);}
};

.....
std::partial_sum
{
boost::make_transform_iterator(x.begin(),convert<Integer,int>()),
boost::make_transform_itertator(x.end(),convert<Integer,int>()),
y.begin(),
std::multiplies<Integer>()
);

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.