2003-02-12 22:00:52 +00:00
|
|
|
#include <boost/ref.hpp>
|
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
class generate_zero {
|
|
|
|
public:
|
|
|
|
typedef int result_type;
|
|
|
|
generate_zero() {}
|
|
|
|
int operator()() const { return 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
generate_zero(const generate_zero&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class generate_zero_no_result_type {
|
|
|
|
public:
|
|
|
|
generate_zero_no_result_type() {}
|
|
|
|
int operator()() const { return 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
generate_zero_no_result_type(const generate_zero_no_result_type&);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void check_generate_zero(F f)
|
|
|
|
{
|
|
|
|
assert(f() == 0);
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
class negate_with_result_type
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int result_type;
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
negate_with_result_type() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()(int x) const { return -x; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
negate_with_result_type(const negate_with_result_type&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
class negate_with_result_of
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
2003-02-23 00:26:46 +00:00
|
|
|
struct result_of
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
typedef int type;
|
|
|
|
};
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
negate_with_result_of() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()(int x) const { return -x; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
negate_with_result_of(const negate_with_result_of&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void check_negate(F f)
|
|
|
|
{
|
|
|
|
int x = 5;
|
|
|
|
assert(f(x) == -x);
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
class add_with_result_type
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int result_type;
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
add_with_result_type() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()(int x, int y) const { return x + y; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
add_with_result_type(const add_with_result_type&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
class add_with_result_of
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-23 00:26:46 +00:00
|
|
|
template<typename F> struct result_of { typedef int type; };
|
2003-02-12 22:00:52 +00:00
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
add_with_result_of() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()(int x, int y) const { return x + y; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
add_with_result_of(const add_with_result_of&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void check_sum(F f)
|
|
|
|
{
|
|
|
|
int x = 3;
|
|
|
|
int y = 5;
|
|
|
|
assert(f(x, y) == x+y);
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
struct zero_negate_add_result_type
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int result_type;
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
zero_negate_add_result_type() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()() const { return 0; }
|
|
|
|
int operator()(int x) const { return -x; }
|
|
|
|
int operator()(int x, int y) const { return x+y; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
zero_negate_add_result_type(const zero_negate_add_result_type&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
struct zero_negate_add_result_of
|
2003-02-12 22:00:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-23 00:26:46 +00:00
|
|
|
template<typename F> struct result_of { typedef int type; };
|
2003-02-12 22:00:52 +00:00
|
|
|
|
2003-02-23 00:26:46 +00:00
|
|
|
zero_negate_add_result_of() {}
|
2003-02-12 22:00:52 +00:00
|
|
|
int operator()() const { return 0; }
|
|
|
|
int operator()(int x) const { return -x; }
|
|
|
|
int operator()(int x, int y) const { return x+y; }
|
|
|
|
|
|
|
|
private:
|
2003-02-23 00:26:46 +00:00
|
|
|
zero_negate_add_result_of(const zero_negate_add_result_of&);
|
2003-02-12 22:00:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Arity zero function objects
|
|
|
|
generate_zero gz;
|
|
|
|
generate_zero_no_result_type gznrt;
|
|
|
|
check_generate_zero(boost::ref(gz));
|
2003-02-23 00:26:46 +00:00
|
|
|
boost::ref(gznrt);
|
2003-02-12 22:00:52 +00:00
|
|
|
|
|
|
|
// Arity 1 function objects
|
2003-02-23 00:26:46 +00:00
|
|
|
negate_with_result_type nrt;
|
|
|
|
negate_with_result_of nro;
|
|
|
|
check_negate(boost::ref(nrt));
|
|
|
|
check_negate(boost::ref(nro));
|
2003-02-12 22:00:52 +00:00
|
|
|
|
|
|
|
// Arity 2 function objects
|
2003-02-23 00:26:46 +00:00
|
|
|
add_with_result_type art;
|
|
|
|
add_with_result_of aro;
|
|
|
|
check_sum(boost::ref(art));
|
|
|
|
check_sum(boost::ref(aro));
|
2003-02-12 22:00:52 +00:00
|
|
|
|
|
|
|
// Arity overloading in function objects
|
2003-02-23 00:26:46 +00:00
|
|
|
zero_negate_add_result_type znart;
|
|
|
|
zero_negate_add_result_type znaro;
|
|
|
|
check_generate_zero(boost::ref(znart));
|
|
|
|
check_negate(boost::ref(znart));
|
|
|
|
check_sum(boost::ref(znart));
|
|
|
|
check_generate_zero(boost::ref(znaro));
|
|
|
|
check_negate(boost::ref(znaro));
|
|
|
|
check_sum(boost::ref(znaro));
|
2003-02-12 22:00:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|