---
name: "foreach"
section: "3bobcat"
platform: "linux"
package: "libbobcat-dev"
version: "2.08.01-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/foreach/3bobcat"
---

# foreach(3bobcat)

Extensions to the STL for\_each function template

## NAME

FBB::for\_each - Extensions to the STL for\_each function template

## SYNOPSIS

**#include \<bobcat/foreach\>**\

## DESCRIPTION

The **FBB::for\_each** function templates extend the STL *for\_each* function template allowing its use in many often occurring situations.

The STL *for\_each* function template requires a pair of iterators preventing its use in situations where an action should be performed a number of times using a plain, non-dereferenceable variable rather than an iterator. Furthermore, the STL template requires the use of either a classless or static class member function or a function object. In situations in which a class member is called a number of times the STL template function cannot easily be used.

The *FBB::for\_each* function template was designed to allow the use of the *for\_each* algorithm in the abovementioned situations. It cannot be used to pass a \`local function context’ to the function performing the action and it cannot be used to call a function a number of times. In the former case the *FBB::FnWrap1c* and *FBB::FnWrap2c* classes can be used; in the latter case the [**repeat**(3bobcat)](/repeat/3bobcat.md) function templates can be used.

All *FBB::for\_each* template functions are defined *inline*, allowing the compiler to \`optimize away’ the *for\_each* function call  itself.

Since some of the *FBB::for\_each* function templates use the same number of parameters as the *stl::for\_each* function templates the explicit use of the *FBB* namespace will often be required in situations where both function templates are made available to the compiler.

## NAMESPACE

**FBB**\
 All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace **FBB**.

## INHERITS FROM

\-

## OVERLOADED FUNCTIONS

In the following description several template type parameters are used. They are:

- o

  **Iter** represents an iterator type;

- o

  **Function** represents a pointer to a classless or static class member function or to a function object;

- o

  **Class** represents a class type;

- o

  **Member** represents a pointer to a *Class* member function expecting a (reference to a) derefenced *Iter* object or an argument of the same type as the first *for\_each* parameter.

- o

  **void for\_each(Iter begin, Iter end, Function &fun)**:\
   *fun* is called *end - begin* times, passing *begin* or *\*begin* with *begin* iterating towards *end* to *fun*. If *begin* and *end* are class- or pointer type variables *\*begin* is passed to *fun* otherwise *begin* is passed to *fun*.

- o

  **void for\_each(Iter begin, Iter end, Class &object, Member member)**:\
   *object.\*member* is called *end - begin* times, passing *begin* or *\*begin* with *begin* iterating towards *end* to *object.\*member*. If *begin* and *end* are class- or pointer type variables *\*begin* is passed to *fun* otherwise *begin* is passed to *fun*.

- o

  **void for\_each(Iter begin, Iter end, Class \*object, Member member)**:\
   *object-\>\*member* is called *end - begin* times, passing *begin* or *\*begin* with *begin* iterating towards *end* to *object-\>\*member*. If *begin* and *end* are class- or pointer type variables *\*begin* is passed to *fun* otherwise *begin* is passed to *fun*. *Class* and *Member* may also be defined as *const* objects.

## EXAMPLES

Assume a class *CGIFSA* defines the following inline function, using the array *bool d\_escape\[\]* and the variable *bool d\_setEscape*:

```
    inline void CGIFSA::setEscape(size_t idx)
    {
        d_escape[idx] = d_setEscape;
    }
```

To set a series of *d\_escape* elements to *d\_setEscape* a member function of the class *CGIFSA* may use the following code (with *size\_t* *begin, end*):

```
    if (begin <= end)
        for_each(begin, end + 1, this, &CGIFSA::setEscape);
```

## FILES

*bobcat/foreach* - defines the template functions

## SEE ALSO

[**bobcat**(7)](/bobcat/7.md), [**fnwrap1**(3bobcat)](/fnwrap1/3bobcat.md), [**fnwrap1c**(3bobcat)](/fnwrap1c/3bobcat.md),  , [**fnwrap2**(3bobcat)](/fnwrap2/3bobcat.md), [**fnwrap2c**(3bobcat)](/fnwrap2c/3bobcat.md),  [**repeat**(3bobcat)](/repeat/3bobcat.md)

## BUGS

Note that the *for\_each* algorithms iterate until *begin* equals *end*; hence the test for *begin \<= end* in the above example.

## DISTRIBUTION FILES

- o

  *bobcat\_2.08.01-x.dsc*: detached signature;

- o

  *bobcat\_2.08.01-x.tar.gz*: source archive;

- o

  *bobcat\_2.08.01-x\_i386.changes*: change log;

- o

  *libbobcat1\_2.08.01-x\_\*.deb*: debian package holding the libraries;

- o

  *libbobcat1-dev\_2.08.01-x\_\*.deb*: debian package holding the libraries, headers and manual pages;

- o

  *http://sourceforge.net/projects/bobcat*: public archive location;

## BOBCAT

Bobcat is an acronym of \`Brokken’s Own Base Classes And Templates’.

## COPYRIGHT

This is free software, distributed under the terms of the  GNU General Public License (GPL).

## AUTHOR

Frank B. Brokken (**f.b.brokken@rug.nl**).
