-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexample.cpp
More file actions
56 lines (44 loc) · 951 Bytes
/
example.cpp
File metadata and controls
56 lines (44 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Function.h"
#include <iostream>
int function(std::ostream& os)
{
os << "Free function\n";
return 0;
}
struct Class
{
int operator()(std::ostream& os)
{
os << "Class::operator()\n";
return 1;
}
int fun(std::ostream& os)
{
os << "Class::fun()\n";
return 2;
}
};
int main(int argc, char *argv[]) {
int foo = 1;
double bar = 3;
Function<int (std::ostream &), 128> f([=](std::ostream &os) {
os << "test " << foo << " " << bar << std::endl;
return foo;
});
f(std::cout);
f = &function;
f(std::cout);
f = function;
f(std::cout);
Class cl;
f = std::bind(&Class::fun, &cl, std::placeholders::_1);
f(std::cout);
f = cl;
f(std::cout);
f = nullptr;
try { f(std::cout); }
catch (std::bad_function_call const&) { }
std::cout << "Assigned nullptr, function is " << (f? "not " : "") << "empty\n";
std::cout << "Size of Function: " << sizeof(f) << '\n';
return 0;
}