mirror of
https://github.com/boostorg/function.git
synced 2025-07-23 17:37:14 +02:00
Testcases autogenerated from the Boost.Function tutorial
[SVN r16711]
This commit is contained in:
38
test/function_arith_cxx98.cpp
Normal file
38
test/function_arith_cxx98.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
float mul_ints(int x, int y) { return ((float)x) * y; }
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function<float (int x, int y)> f;
|
||||||
|
|
||||||
|
|
||||||
|
struct int_div {
|
||||||
|
float operator()(int x, int y) const { return ((float)x)/y; };
|
||||||
|
};
|
||||||
|
|
||||||
|
f = int_div();
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << f(5, 3) >> std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
if (f)
|
||||||
|
std::cout << f(5, 3) << std::endl;
|
||||||
|
else
|
||||||
|
std::cout << "f has no target, so it is unsafe to call" << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
f = 0;
|
||||||
|
|
||||||
|
|
||||||
|
f = &mul_ints;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
37
test/function_arith_portable.cpp
Normal file
37
test/function_arith_portable.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
float mul_ints(int x, int y) { return ((float)x) * y; }
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function2<float, int, int> f;
|
||||||
|
|
||||||
|
|
||||||
|
struct int_div {
|
||||||
|
float operator()(int x, int y) const { return ((float)x)/y; };
|
||||||
|
};
|
||||||
|
|
||||||
|
f = int_div();
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << f(5, 3) >> std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
if (f)
|
||||||
|
std::cout << f(5, 3) << std::endl;
|
||||||
|
else
|
||||||
|
std::cout << "f has no target, so it is unsafe to call" << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
f = 0;
|
||||||
|
|
||||||
|
|
||||||
|
f = &mul_ints;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
19
test/function_ref_cxx98.cpp
Normal file
19
test/function_ref_cxx98.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
struct stateful_type { int operator(int) const { return 0; } };
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
stateful_type a_function_object;
|
||||||
|
boost::function<int (int)> f;
|
||||||
|
f = boost::ref(a_function_object);
|
||||||
|
|
||||||
|
boost::function<int (int)> f2(f);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
19
test/function_ref_portable.cpp
Normal file
19
test/function_ref_portable.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
struct stateful_type { int operator(int) const { return 0; } };
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
stateful_type a_function_object;
|
||||||
|
boost::function1<int, int> f;
|
||||||
|
f = boost::ref(a_function_object);
|
||||||
|
|
||||||
|
boost::function1<int, int> f2(f);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
23
test/mem_fun_cxx98.cpp
Normal file
23
test/mem_fun_cxx98.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int foo(int);
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function<int (X*, int)> f;
|
||||||
|
|
||||||
|
f = &X::foo;
|
||||||
|
|
||||||
|
X x;
|
||||||
|
f(&x, 5);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
23
test/mem_fun_portable.cpp
Normal file
23
test/mem_fun_portable.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int foo(int);
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function2<int, X*, int> f;
|
||||||
|
|
||||||
|
f = &X::foo;
|
||||||
|
|
||||||
|
X x;
|
||||||
|
f(&x, 5);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
22
test/std_bind_cxx98.cpp
Normal file
22
test/std_bind_cxx98.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int foo(int);
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function<int (int)> f;
|
||||||
|
X x;
|
||||||
|
f = std::bind1st(std::mem_fun(&X::foo), &x);
|
||||||
|
|
||||||
|
f(5); // Call x.foo(5)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
22
test/std_bind_portable.cpp
Normal file
22
test/std_bind_portable.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int foo(int);
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function1<int, int> f;
|
||||||
|
X x;
|
||||||
|
f = std::bind1st(std::mem_fun(&X::foo), &x);
|
||||||
|
|
||||||
|
f(5); // Call x.foo(5)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
24
test/sum_avg_cxx98.cpp
Normal file
24
test/sum_avg_cxx98.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
void do_sum_avg(int values[], int n, int& sum, float& avg)
|
||||||
|
{
|
||||||
|
sum = 0;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
sum += values[i];
|
||||||
|
avg = (float)sum / n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function<void (int values[], int n, int& sum, float& avg)> sum_avg;
|
||||||
|
|
||||||
|
|
||||||
|
sum_avg = &do_sum_avg;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
24
test/sum_avg_portable.cpp
Normal file
24
test/sum_avg_portable.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
void do_sum_avg(int values[], int n, int& sum, float& avg)
|
||||||
|
{
|
||||||
|
sum = 0;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
sum += values[i];
|
||||||
|
avg = (float)sum / n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
boost::function4<void, int[], int, int&, float> sum_avg;
|
||||||
|
|
||||||
|
|
||||||
|
sum_avg = &do_sum_avg;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user