|
Boost : |
From: Hubert Holin (Hubert.Holin_at_[hidden])
Date: 2003-01-31 07:38:53
Somewhere in the E.U., le 31/01/2003
Bonjour
Given my recent (and now solved, thanks!) problems with the
Boost.Test framework and bind, I felt I needed some exercice. As I
believe this may be of interest to others, and do not know where to put
it where it can be found (perhaps included in bind's doc?), I will
reproduce it below (hoping this will not be a waste of bandwidth).
Encore merci!
HH
-- 8>< ------------------------------------ ><8 --
// A simple exercice using funtional, function and bind...
#include <iostream>
#include <functional>
#include <boost/bind.hpp>
#include <boost/function.hpp>
void eater(::boost::function0<void> const & snack)
{
::std::cout << "Eating a ";
snack();
}
void f()
{
::std::cout << "function of no parameters." << ::std::endl;
}
void fn(int)
{
::std::cout << "normal function of one parameter." << ::std::endl;
}
template<typename T>
void ft(int)
{
::std::cout << "templated function of one parameter." << ::std::endl;
}
class A
{
public:
A(char const * blurb)
: wit(blurb)
{
}
void operator ()() const
{
::std::cout << "member function of no parameters of "
<< wit << "." << ::std::endl;
}
::std::string wit;
};
class AN
{
public:
AN(char const * blurb)
: wit(blurb)
{
}
void operator ()(int) const
{
::std::cout << "member function of one parameter of "
<< wit << " (normal)." << ::std::endl;
}
::std::string wit;
};
template<typename T>
class AT
{
public:
AT(char const * blurb)
: wit(blurb)
{
}
void operator ()(int) const
{
::std::cout << "member function of one parameter of "
<< wit << " (templated)." << ::std::endl;
}
::std::string wit;
};
int main(int argc, char * argv[])
{
eater(&f);
eater(::boost::bind(&fn, 1));
eater(::boost::bind(static_cast< void (*) (int) >(&ft<int>), 1));
A a("a named instance of a class");
eater(::boost::bind(std::mem_fun_ref(&(A::operator ())), a));
eater(::boost::bind(std::mem_fun_ref(&(A::operator ())), A("an
anonymous instance of a class")));
AN an("a named instance of a class");
eater(::boost::bind(std::mem_fun_ref(&(AN::operator ())), an, 1));
eater(::boost::bind(std::mem_fun_ref(&(AN::operator ())), AN("an
anonymous instance of a class"), 1));
AT<int> at("a named instance of a class");
eater(::boost::bind(std::mem_fun_ref(&(AT<int>::operator ())), at,
1));
eater(::boost::bind(std::mem_fun_ref(&(AT<int>::operator ())),
AT<int>("an anonymous instance of a class"), 1));
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk