M
Michael Le Barbier Grünewald
Dear group,
interpreting iterators as handles to stream, it seems natural to try to represent
filters, combinators, and so on, as iterators as well. Being new to C++, Iam looking for advice on how to do the things right.
Let's say I want to define a filter for vector of integers, that is, implement the following signature:
class filter : public vector<int>::const_iterator
{
filter(bool predicate(int));
// Move to the next integer satisfying predicate
filter& operator++();
};
Here are my difficulties:
— A vector<int>::const_iterator does not know about the structure it is exploring, so I probably need to supply the corresponding (vector.end()) to my cosntructor (so that ++ knows where to stop). I feel it is a bit clumsy, is there a nicer way to deal with this?
— Passing function pointers as arguments is not so general as we may wantit, since the predicate could also be a member function or another callable (and object or a struct with () operator), so I am not quite sure about what I should use as a parameter.
For this second issue, I think I would like the predicate to be either:
1. or a plain function;
2. or a member function on some object;
3. or the result of partial application (with these bind1 and bind2 operators from the STL).
Probably, I could manage to write something for 1. and 2., but there is probably many ways to do this and I am pretty sure I am unable to satisfy 3. with a solid design.
I come from functional languages (where the problem is simply discarded!) so I feel very attracted by functional traits in C++ (these for_each and bind* templates for instance). But being a novice C++ programmer, I am not sure how will these facilities fit in larger projects. (I have the intuition that these facilities will not be usable at all in poorly designed programs..) So I would be very interested to know about your experiences in this domain or to know where I can read interesting or well-written code using these functional traits in C++.
Thanks for your help and your insights!
Michael
interpreting iterators as handles to stream, it seems natural to try to represent
filters, combinators, and so on, as iterators as well. Being new to C++, Iam looking for advice on how to do the things right.
Let's say I want to define a filter for vector of integers, that is, implement the following signature:
class filter : public vector<int>::const_iterator
{
filter(bool predicate(int));
// Move to the next integer satisfying predicate
filter& operator++();
};
Here are my difficulties:
— A vector<int>::const_iterator does not know about the structure it is exploring, so I probably need to supply the corresponding (vector.end()) to my cosntructor (so that ++ knows where to stop). I feel it is a bit clumsy, is there a nicer way to deal with this?
— Passing function pointers as arguments is not so general as we may wantit, since the predicate could also be a member function or another callable (and object or a struct with () operator), so I am not quite sure about what I should use as a parameter.
For this second issue, I think I would like the predicate to be either:
1. or a plain function;
2. or a member function on some object;
3. or the result of partial application (with these bind1 and bind2 operators from the STL).
Probably, I could manage to write something for 1. and 2., but there is probably many ways to do this and I am pretty sure I am unable to satisfy 3. with a solid design.
I come from functional languages (where the problem is simply discarded!) so I feel very attracted by functional traits in C++ (these for_each and bind* templates for instance). But being a novice C++ programmer, I am not sure how will these facilities fit in larger projects. (I have the intuition that these facilities will not be usable at all in poorly designed programs..) So I would be very interested to know about your experiences in this domain or to know where I can read interesting or well-written code using these functional traits in C++.
Thanks for your help and your insights!
Michael