public member function
<functional>
std::function::function default/empty (1)function() noexcept;function (nullptr_t fn) noexcept;initialization (2)
template <class Fn> function (Fn fn);copy (3)
function (const function& x);move (4)
function (function&& x);with allocator (5)
template <class Alloc> function (allocator_arg_t aa, const Alloc& alloc) noexcept;template <class Alloc> function (allocator_arg_t aa, const Alloc& alloc, nullptr_t fn) noexcept;template <class Fn, class Alloc> function (allocator_arg_t aa, const Alloc& alloc, Fn fn);template <class Alloc> function (allocator_arg_t aa, const Alloc& alloc, const function& x);template <class Alloc> function (allocator_arg_t aa, const Alloc& alloc, function&& x);
Construct function wrapper
Constructs a function object:fn shall be callable for the arguments and return type specified as template arguments for the class.
If fn is not callable for the arguments and return type specified as template arguments for the class, this constructor does not participate in overload resolution.
x.target()
).
std::move(fn)
).
*this
.
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
// function example
#include <iostream> // std::cout
#include <functional> // std::function, std::negate
// a function:
int half(int x) {return x/2;}
// a function object class:
struct third_t {
int operator()(int x) {return x/3;}
};
// a class with data members:
struct MyValue {
int value;
int fifth() {return value/5;}
};
int main () {
std::function<int(int)> fn1 = half; // function
std::function<int(int)> fn2 = ½ // function pointer
std::function<int(int)> fn3 = third_t(); // function object
std::function<int(int)> fn4 = [](int x){return x/4;}; // lambda expression
std::function<int(int)> fn5 = std::negate<int>(); // standard function object
std::cout << "fn1(60): " << fn1(60) << '\n';
std::cout << "fn2(60): " << fn2(60) << '\n';
std::cout << "fn3(60): " << fn3(60) << '\n';
std::cout << "fn4(60): " << fn4(60) << '\n';
std::cout << "fn5(60): " << fn5(60) << '\n';
// stuff with members:
std::function<int(MyValue&)> value = &MyValue::value; // pointer to data member
std::function<int(MyValue&)> fifth = &MyValue::fifth; // pointer to member function
MyValue sixty {60};
std::cout << "value(sixty): " << value(sixty) << '\n';
std::cout << "fifth(sixty): " << fifth(sixty) << '\n';
return 0;
}
fn1(60): 30 fn2(60): 30 fn3(60): 20 fn4(60): 15 fn5(60): -60 value(sixty): 60 fifth(sixty): 12
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4