C
Chris Gordon-Smith
Hello
After using STL containers for several years, I finally decided to 'do
the right thing' and start using STL algorithms and function objects.
Things turned out to be a little more complicated than I thought. I
wanted to have a 'for_each()' to which I pass iterators to items from an
std::list. Each item is itself an std::list, although this is not
important for the question I want to raise.
My first attempt looked like this:
Perturb_Conf_Checker Checker(this, // Checker is a
AtomLoc1, // function object
AtomLoc2);
for_each(BondChars_Pt->Perturbing_Config_Ls_.begin(),
BondChars_Pt->Perturbing_Config_Ls_.end(),
Checker);
TRACEVAL(Checker.Get_Match_Fl()); // TRACEVAL is a debug macro
'Checker' is a function object that tests each std::list passed to it for
a match on a particular condition. I want to retrieve the final result of
this sequence of tests from the function object by calling
Checker.Get_Match_Fl().
It turns out that this can't be done with the above code, because the
function object (Checker) is passed to for_each() by value, so that the
Perturb_Conf_Checker that for_each() works is a copy of Checker in my
code.
A look at my STL book (Josuttis 1999) indicates that the problem can be
dealt with by instantiating for_each() with template parameters. The
following code works:
Perturb_Conf_Checker Checker(this, AtomLoc1, AtomLoc2);
for_each <std::_List_iterator<list <BondPerturb_AtomSpec*> >,
Perturb_Conf_Checker&>
(BondChars_Pt->Perturbing_Config_Ls_.begin(),
BondChars_Pt->Perturbing_Config_Ls_.end(),
Checker);
TRACEVAL(Checker.Get_Match_Fl());
My question is this: Is std::_List_iterator intended for use in user
code? Is it portable? That leading underscore looks a bit odd. I don't
see std::_List_iterator in any of my books (although its true that some
of them are a few years old).
Replacing 'std::_List_iterator' with 'input_iterator' doesn't compile
(gcc version 4.3.2 (Debian 4.3.2-1.1)).
If my code is not correct, then what is? (I prefer not to return the
function object from for_each()).
Chris Gordon-Smith
www.simsoup.info
After using STL containers for several years, I finally decided to 'do
the right thing' and start using STL algorithms and function objects.
Things turned out to be a little more complicated than I thought. I
wanted to have a 'for_each()' to which I pass iterators to items from an
std::list. Each item is itself an std::list, although this is not
important for the question I want to raise.
My first attempt looked like this:
Perturb_Conf_Checker Checker(this, // Checker is a
AtomLoc1, // function object
AtomLoc2);
for_each(BondChars_Pt->Perturbing_Config_Ls_.begin(),
BondChars_Pt->Perturbing_Config_Ls_.end(),
Checker);
TRACEVAL(Checker.Get_Match_Fl()); // TRACEVAL is a debug macro
'Checker' is a function object that tests each std::list passed to it for
a match on a particular condition. I want to retrieve the final result of
this sequence of tests from the function object by calling
Checker.Get_Match_Fl().
It turns out that this can't be done with the above code, because the
function object (Checker) is passed to for_each() by value, so that the
Perturb_Conf_Checker that for_each() works is a copy of Checker in my
code.
A look at my STL book (Josuttis 1999) indicates that the problem can be
dealt with by instantiating for_each() with template parameters. The
following code works:
Perturb_Conf_Checker Checker(this, AtomLoc1, AtomLoc2);
for_each <std::_List_iterator<list <BondPerturb_AtomSpec*> >,
Perturb_Conf_Checker&>
(BondChars_Pt->Perturbing_Config_Ls_.begin(),
BondChars_Pt->Perturbing_Config_Ls_.end(),
Checker);
TRACEVAL(Checker.Get_Match_Fl());
My question is this: Is std::_List_iterator intended for use in user
code? Is it portable? That leading underscore looks a bit odd. I don't
see std::_List_iterator in any of my books (although its true that some
of them are a few years old).
Replacing 'std::_List_iterator' with 'input_iterator' doesn't compile
(gcc version 4.3.2 (Debian 4.3.2-1.1)).
If my code is not correct, then what is? (I prefer not to return the
function object from for_each()).
Chris Gordon-Smith
www.simsoup.info