Re: Unary functor on algorithm using copy constructor
"Dijkstra" <pepitek@gmail.com> wrote in message
news:6b6e9f64-15c8-40a0-92f4-c4dfe7540ce1@k30g2000hse.googlegroups.com...
Hi folks!
First, this is the code I'm using to expose the problem:
------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>
using namespace std;
struct space_comparer : public unary_function<char, bool> {
mutable argument_type __lastChar;
If you need one, and only one __lastChar (a bad choice of names, by the way)
then you should make it static. I don't think that mutable makes it static.
static argument_type lastChar_;
Underscores in the front of variable names are generally reerved for the
compiler,t here are different rules as to which are and which aren't, it is
best to just never use preceeding underscore for you own variables.
space_comparer()
{
__lastChar = 'X';
cout << "ctor() called and lastChar is '" << __lastChar << "'" <<
endl;
}
space_comparer(const space_comparer &__src)
{
cout << "copy ctor() called setting lastChar to 'Y'" << endl;
__lastChar = 'Y';
}
result_type operator ()(const argument_type c) const
{
cout << "[" << __lastChar << " | " << c << "]" << endl;
__lastChar = c;
return ::isspace(c);
}
};
main()
{
string cad1;
string cad2;
const space_comparer instance;
cad1 = "cpp";
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
return 0;
}
---------------------------------------------------------------
OK, so the meaning of all that is to use a unary function with a
persistent "state", so that the "space_comparer" works by using
previous values. My problem is that the "remove_copy_if" algorithm,
and I think all algorithms, use a copy.
I mean, suppose this call:
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
space_comparer() );
This works by constructing a new "space_comparer" functor object and
using its instance for all the algorithm lifetime. What I like to do
is use an actual instance as the predicate, so that it can mantain a
state across algorithm calls.
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
I supposed the above call would reuse the "instance" of the object
functor, which indeed does. But it does a temporary copy of the object
and it works with it, leaving the original instance untouched. The
output of the above code is:
-----------------------------------------------------------------
ctor() called and lastChar is 'X'
copy ctor() called setting lastChar to 'Y'
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to 'Y'
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to 'Y'
[Y | c]
[c | p]
[p | p]
-----------------------------------------------------------------
But, the expected output needs to be like this:
-----------------------------------------------------------------
ctor() called and lastChar is 'X'
[X | c]
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
-----------------------------------------------------------------
I don't know if this is at all possible. I already tried with
"mem_func" and such techniques, but to no avail...
Please, I'm not so STL literate, so bear with me. :-)
Cheers,