-
Notifications
You must be signed in to change notification settings - Fork 139
/
cpp17-invoke.cpp
32 lines (28 loc) · 928 Bytes
/
cpp17-invoke.cpp
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
#include <iostream>
#include <string>
#include <ostream>
// std::invoke is provide by header functional
#include <functional>
struct Dummy{
double evalme(double x) {
std::cerr << __FILE__ << ":" << __LINE__ << " I was evaluated ; 2x = " << 2 *x << '\n';
return 2 * x;
}
double operator()(double x){
std::cerr << __FILE__ << ":" << __LINE__ << " Call function-operator << 4 * x = " << 4 * x << '\n';
return 4 * x;
}
};
double computeDouble(double x){
std::cerr << __FILE__ << ":" << __LINE__ << " Computed double of 2x = " << 2 * x << '\n';
return 2 * x;
}
int main(){
std::invoke(computeDouble, 3.0);
Dummy dummy;
std::invoke(dummy, 3.0);
std::invoke(Dummy(), 2.0);
// Call method: .evalme indirectly
std::invoke(&Dummy::evalme, dummy, 3.0);
return 0;
}