forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_types.cpp
More file actions
50 lines (39 loc) · 1.06 KB
/
function_types.cpp
File metadata and controls
50 lines (39 loc) · 1.06 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
// Regular function
int add(int a, int b) {
return a + b;
}
// Templated function
template <typename T>
T multiply(T a, T b) {
return a * b;
}
class Calculator {
public:
// Static member function
static int subtract(int a, int b) {
return a - b;
}
// Method using a local lambda
void printSum(const std::vector<int>& nums) {
auto printer = [](int val) { std::cout << val << std::endl; };
for (int n : nums) printer(n);
}
};
int main() {
Calculator calc;
std::vector<int> numbers = {1, 2, 3};
calc.printSum(numbers);
// Standalone lambda
auto lambda = [](int a, int b) { return a + b; };
std::cout << "Lambda sum: " << lambda(3,4) << std::endl;
// Regular function call
std::cout << "Add: " << add(2,3) << std::endl;
// Templated function call
std::cout << "Multiply: " << multiply(2,3) << std::endl;
// Static member function call
std::cout << "Subtract: " << Calculator::subtract(5,2) << std::endl;
return 0;
}