Skip to content

Commit b848d78

Browse files
authored
Merge pull request #31 from urbytes21/dev_branch
id 1769574242
2 parents 19694c0 + 8b4ac54 commit b848d78

5 files changed

Lines changed: 212 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ set(PROJECT_NAME_TEST ${PROJECT_NAME}_unit_test) # name for the unit-test execut
2525
# Compiler and language configuration
2626
# ----------------------------------------------------------------------------------------
2727
# Require C++17 for GoogleTest and modern C++ features
28-
set(CMAKE_CXX_STANDARD 17)
28+
set(CMAKE_CXX_STANDARD 20)
2929
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3030

3131
# Optionally enforce warnings (good for learning/debugging)
@@ -124,6 +124,8 @@ set(APP_SOURCES
124124
"src/core/datatypes/container/sequence/Array.cpp"
125125
"src/core/datatypes/container/sequence/Vector.cpp"
126126
"src/core/datatypes/container/unordered/UnorderedMap.cpp"
127+
"src/core/expression/Lambda.cpp"
128+
"src/core/expression/FunctionPointer.cpp"
127129
)
128130

129131
# Test files
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
5+
namespace {
6+
7+
int increase(int a, int b) {
8+
return a > b;
9+
}
10+
11+
int decrease(int a, int b) {
12+
return a < b;
13+
}
14+
15+
// // Function
16+
// // Declaring
17+
// return_type (*FuncPtr) (parameter type, ....);
18+
typedef int (*SortFcn)(int a, int b);
19+
20+
void run() {
21+
std::vector<int> vect{1, 6, 4, 22, 0, 6, 33, 39, -5};
22+
23+
auto f_print = [](const std::vector<int>& vec) {
24+
for (const auto& e : vec) {
25+
std::cout << e << " ";
26+
}
27+
std::cout << "\n";
28+
};
29+
30+
std::cout << "Before sorting : \n";
31+
f_print(vect);
32+
33+
std::cout << "Sorting in descending "
34+
<< "order \n";
35+
36+
// Use auto
37+
auto sortTypeAuto = increase;
38+
std::sort(vect.begin(), vect.end(), sortTypeAuto);
39+
40+
// Use pointer
41+
SortFcn sortTypePtr = decrease;
42+
f_print(vect);
43+
44+
std::cout << "Sorting with absolute "
45+
<< "value as parameter\n ";
46+
std::sort(vect.begin(), vect.end(), sortTypePtr);
47+
48+
for (auto i : vect)
49+
std::cout << i << " ";
50+
std::cout << "\n";
51+
}
52+
} // namespace
53+
54+
struct FunctionPointer {
55+
FunctionPointer() {
56+
std::cout << "\n--- Function Pointer Example ---\n";
57+
run();
58+
}
59+
};
60+
61+
static FunctionPointer autoRunner;

src/core/expression/Lambda.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <algorithm> // for std::sort
2+
// std::sort
3+
// default (1)
4+
// template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last);
5+
// custom (2)
6+
// template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
7+
8+
#include <iostream>
9+
#include <vector>
10+
11+
namespace {
12+
void run() {
13+
std::vector<int> vect{-1, -6, 4, 2, 0, 6, 3, 9, -5};
14+
15+
auto f_print = [](const std::vector<int>& vec) {
16+
for (const auto& e : vec) {
17+
std::cout << e << " ";
18+
}
19+
std::cout << "\n";
20+
};
21+
22+
std::cout << "Before sorting : \n";
23+
f_print(vect);
24+
25+
std::cout << "Sorting in descending "
26+
<< "order \n";
27+
std::sort(vect.begin(), vect.end(), [](int a, int b) { return a > b; });
28+
29+
f_print(vect);
30+
31+
std::cout << "Sorting with absolute "
32+
<< "value as parameter\n ";
33+
std::sort(vect.begin(), vect.end(), [](int a, int b) { return a < b; });
34+
35+
for (auto i : vect)
36+
std::cout << i << " ";
37+
std::cout << "\n";
38+
}
39+
} // namespace
40+
41+
struct LambdaRunner {
42+
LambdaRunner() {
43+
std::cout << "\n--- Lambda Expression Example ---\n";
44+
run();
45+
}
46+
};
47+
48+
static LambdaRunner autoRunner;

src/core/expression/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Theory
2+
3+
## 1. Lambda Expressions
4+
- In C++11 and later, lambda is a convenient way of defining an anonymous function object right at the location where it's involked or passed as an argument to a function.
5+
6+
- Syntax:
7+
```cpp
8+
[=] () mutable throw() -> int
9+
{
10+
int n = x + y;
11+
return n;
12+
}
13+
14+
```
15+
- `[=]`: capture clause a.k.a lambda introducer
16+
- `()`: (O) pararam list a.k.a lambda declarator
17+
- `mutable`: (O)
18+
- `throw()`: (O)
19+
- `-> int`: (O) trailing-return-type
20+
- body
21+
22+
### 1.1 Capture Clause
23+
- It uses to introduce new variables in its body, specifics which vars are captured, and whether the capture is `by value[=]` or `by reference [&]`.
24+
- An empty capture clause `[]` indicates that the body accesses no vars in the enclosing scope.
25+
- An identifier or `this` cannot appear more than once in a capture scope.
26+
- Since C++14, we can introduce and initialize new vars in the capture scope.
27+
- E.g.
28+
```cpp
29+
int a{};
30+
int b{};
31+
32+
auto f = []{ // no capture
33+
return 1;
34+
}
35+
36+
auto f0 = [a]{ // capture by value
37+
return a+1;
38+
}
39+
40+
auto f1 = [&a]{
41+
return a+=1; // capture by reference (a = 1)
42+
}
43+
44+
auto f2 = [=]{
45+
return a + b; // all capture by value
46+
}
47+
48+
auto f3 = [&]{
49+
a+=1;
50+
b+=1;
51+
return a + b; // all capture by reference
52+
}
53+
54+
auto f4 = [int a{}]{ // no capture
55+
return a;
56+
}
57+
```
58+
59+
## 2. Function Pointers
60+
- A function pointer is a pointer variable that stores the address of a function with a specific return type and parameter list.
61+
- Syntax:
62+
```cpp
63+
// Declare
64+
return_type (*FuncPtr) (parameter type, ....);
65+
66+
// Referencing: Assigning a function’s address to the function pointer.
67+
FuncPtr = function_name;
68+
69+
// 3) Dereferencing: Invoking the function using the pointer. The dereference operator * is optional during function calls.
70+
FuncPtr(10, 20); // Preferred
71+
(*FuncPtr)(10, 20); // Also valid
72+
```
73+
74+
- e.g.
75+
```cpp
76+
void print(int a){
77+
std::cout << a;
78+
}
79+
80+
void (*FuncPtr)(int a);
81+
FuncPtr = print;
82+
FuncPtr(1);
83+
```
84+

src/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
#include "version.h"
44

55
int main(int argc, char* argv[]) {
6+
std::cout << std::endl;
7+
if (__cplusplus == 202302L)
8+
std::cout << "C++23";
9+
else if (__cplusplus == 202002L)
10+
std::cout << "C++20";
11+
else if (__cplusplus == 201703L)
12+
std::cout << "C++17";
13+
else if (__cplusplus == 201402L)
14+
std::cout << "C++14";
15+
else if (__cplusplus == 201103L)
16+
std::cout << "C++11";
17+
else if (__cplusplus == 199711L)
18+
std::cout << "C++98";
19+
else
20+
std::cout << "pre-standard C++ or an unknown version: " << __cplusplus;
21+
std::cout << "\n";
622
std::cout << APP_NAME << " v" << APP_VERSION << std::endl;
723
std::cout << APP_DESCRIPTION << std::endl;
824
return 0;

0 commit comments

Comments
 (0)