std::_List_iterator as a Template Parameter?

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
 
Ö

Öö Tiib

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).

What is the type of "BondChars_Pt->Perturbing_Config_Ls_"?
That type typedefs nested "iterator" and "const_iterator" types.
Use them.
 
C

Chris Gordon-Smith

A common trick is to use a function object that holds a pointer or
reference to wherever you want to accumulate the result.

Thanks very much. A reference works fine, and no need for any funny
template parameters.

Chris Gordon-Smith
www.simsoup.info
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top