forked from furkankirac/cs409-2023-24-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b53720a
commit 7d3e0f4
Showing
8 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// std::optional<T> | ||
// = default, = delete, std::declval | ||
// [[nodiscard]], std::unique_ptr, std::shared_ptr | ||
// duck typing? type erasure: std::function | ||
// SFINAE: enable_if, and its relation with concepts | ||
// ADL: argument dependent lookup, friend functions | ||
// CRTP: curiously recurring template pattern | ||
|
||
#include <iostream> | ||
#include <optional> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
// Function to find an element in a vector that meets a certain condition | ||
std::optional<int> findFirstEven(const std::vector<int>& data) { | ||
for (int num : data) { | ||
if (num % 2 == 0) { | ||
return num; // Returns the first even number found | ||
} | ||
} | ||
return {}; // Return an empty std::optional (std::nullopt works as well) | ||
} | ||
|
||
int main() { | ||
std::vector<int> values = {1, 3, 5, 6, 7}; | ||
|
||
auto result = findFirstEven(values); | ||
|
||
if (result) { // Checks if result has a value | ||
std::cout << "First even number: " << *result << std::endl; | ||
} else { | ||
std::cout << "No even number found." << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// = default, = delete, std::declval | ||
// [[nodiscard]], std::unique_ptr, std::shared_ptr | ||
// duck typing? type erasure: std::function | ||
// SFINAE: enable_if, and its relation with concepts | ||
// ADL: argument dependent lookup, friend functions | ||
// CRTP: curiously recurring template pattern | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
|
||
#include <iostream> | ||
#include <optional> | ||
#include <vector> | ||
|
||
//vtable Bar | ||
// index0: 1st function ---> Bar::print | ||
|
||
struct Bar | ||
{ | ||
// void* _vtable; | ||
Bar() { } | ||
|
||
virtual void print() const { | ||
cout << "I am bar" << endl; | ||
} | ||
void doIt() const { | ||
cout << "Just do it" << endl; | ||
} | ||
}; | ||
|
||
//vtable for Foo | ||
// 1st function ---> Foo::print | ||
|
||
struct Foo : Bar | ||
{ | ||
// void* _vtable; | ||
int i; | ||
float f; | ||
|
||
Foo() : i{10}, f{20.2f} { | ||
i = f + 1.0; | ||
} | ||
|
||
Foo(const Foo& other) = default; | ||
Foo(Foo&& other) = default; | ||
|
||
void print() const override { | ||
cout << "I am foo" << endl; | ||
} | ||
|
||
void doIt() const = delete; | ||
}; | ||
|
||
|
||
int main() | ||
{ | ||
auto inst1 = Foo(); | ||
auto inst2 = Bar(); | ||
|
||
|
||
// static_cast<T>, dynamic_cast<T>, reinterpret_cast<T>, const_cast<T> | ||
|
||
auto* inst3 = (Bar*)&inst1; // auto deduces Bar | ||
auto* inst4 = &inst2; // auto deduces Bar | ||
|
||
inst3->print(); // I am foo | ||
inst4->print(); // I am bar | ||
|
||
inst2.doIt(); // works | ||
// inst1.doIt(); // it's deleted | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// std::declval | ||
// [[nodiscard]], std::unique_ptr, std::shared_ptr | ||
// duck typing? type erasure: std::function | ||
// SFINAE: enable_if, and its relation with concepts | ||
// ADL: argument dependent lookup, friend functions | ||
// CRTP: curiously recurring template pattern | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
//struct Foo | ||
//{ | ||
// Foo() = delete; | ||
|
||
// Foo(int) { } | ||
|
||
// void print() const { | ||
// cout << "Hi" << endl; | ||
// } | ||
//}; | ||
|
||
//template<typename T> | ||
//auto callPrintOfType() -> decltype(std::declval<T>().print()) | ||
//{ | ||
//// T{}.print(); | ||
//} | ||
|
||
|
||
#include <utility> | ||
#include <iostream> | ||
|
||
struct Default | ||
{ | ||
int foo() const { return 1; } | ||
}; | ||
|
||
struct NonDefault | ||
{ | ||
NonDefault() = delete; | ||
int foo() const { return 1; } | ||
}; | ||
|
||
int main() | ||
{ | ||
decltype(Default().foo()) n1 = 1; // type of n1 is int | ||
// decltype(NonDefault().foo()) n2 = n1; // error: no default constructor | ||
decltype(std::declval<NonDefault>().foo()) n2 = n1; // type of n2 is int | ||
std::cout << "n1 = " << n1 << '\n' | ||
<< "n2 = " << n2 << '\n'; | ||
} | ||
|
||
//int main() | ||
//{ | ||
// auto foo = Foo{1}; | ||
// foo.print(); | ||
|
||
// callPrintOfType<Foo>(); | ||
|
||
// return 0; | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// [[nodiscard]], std::unique_ptr, std::shared_ptr | ||
// duck typing? type erasure: std::function | ||
// SFINAE: enable_if, and its relation with concepts | ||
// ADL: argument dependent lookup, friend functions | ||
// CRTP: curiously recurring template pattern | ||
|
||
#include <iostream> | ||
#include <memory> | ||
|
||
using namespace std; | ||
|
||
[[nodiscard]] int foo() { | ||
return 1; | ||
} | ||
|
||
[[nodiscard]] unique_ptr<int> bar(unique_ptr<int> i) | ||
{ | ||
return std::move(i); | ||
} | ||
|
||
|
||
struct RGBPixel | ||
{ | ||
char r, g, b; | ||
}; | ||
|
||
template<typename PixelType> | ||
struct Image | ||
{ | ||
size_t height, width; | ||
|
||
PixelType* data; | ||
|
||
Image(size_t height, size_t width) : height{height}, width{width} { | ||
data = new PixelType[height * width]; | ||
} | ||
|
||
~Image() { | ||
delete[] data; | ||
} | ||
|
||
}; | ||
|
||
auto createImage() { | ||
// auto* ptr = new Image<RGBPixel>(100, 100); // C way of doing it | ||
// return ptr; | ||
return make_shared<Image<RGBPixel>>(100, 100); | ||
} | ||
|
||
auto useImage(Image<RGBPixel> *) | ||
{ | ||
} | ||
|
||
template<typename T> | ||
void edge_detect(shared_ptr<Image<T>> img) | ||
{ | ||
} | ||
|
||
int main() | ||
{ | ||
// auto* newImg = createImage(); | ||
// delete newImg; | ||
|
||
// useImage(newImg); | ||
|
||
|
||
// auto img = std::shared_ptr<Image<RGBPixel>>(new Image<RGBPixel>(100, 100)); | ||
auto img = make_shared<Image<RGBPixel>>(100, 100); // allocated the object automatically from heap | ||
edge_detect(img); | ||
|
||
// foo(); // this warns you so that the return value cannot be ignored | ||
|
||
// auto i_ptr = unique_ptr<int>(new int(10)); | ||
// auto i_ptr = make_unique<int>(10); | ||
|
||
// cout << *i_ptr << endl; | ||
|
||
// auto i_ptr2 = std::move(i_ptr); // copy ctor is in "= delete" state | ||
// cout << i_ptr2 << endl; | ||
|
||
// auto j = bar(std::move(i_ptr2)); | ||
|
||
|
||
auto s_ptr = shared_ptr<int>(new int(10)); | ||
// auto s_ptr = make_shared<int>(10); | ||
|
||
// cout << *s_ptr << endl; | ||
|
||
// auto s_ptr2 = s_ptr; // happily copy construct | ||
// cout << *s_ptr << endl; | ||
// cout << *s_ptr2 << endl; | ||
|
||
// auto k = bar(std::move(i_ptr2)); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// duck typing? type erasure: std::function | ||
|
||
// SFINAE: enable_if, and its relation with concepts | ||
// ADL: argument dependent lookup, friend functions | ||
// CRTP: curiously recurring template pattern | ||
|
||
|
||
#include <iostream> | ||
#include <memory> | ||
#include <functional> | ||
|
||
using namespace std; | ||
|
||
struct Foo { void print() const { cout << "I am a duck" << endl; } }; | ||
struct Bar { void print() const { cout << "I am a duck" << endl; } }; | ||
|
||
// free functions | ||
// function objects ---> lambdas | ||
// methods | ||
|
||
double freeFunc1(int) { return 0.0; } | ||
double freeFunc2(int) { return 10.0; } | ||
|
||
struct XYZ { | ||
// int a = 10; | ||
void print() const { cout << "XYZ::print" << endl; } | ||
void print2() const { cout << "XYZ::print2" << endl; } | ||
}; | ||
|
||
//void print2(const XYZ&) { cout << "XYZ::print2" << endl; } | ||
|
||
struct ABC { | ||
void print() const { cout << "ABC::print" << endl; } | ||
}; | ||
|
||
|
||
int main() | ||
{ | ||
// auto foo = Foo{}; | ||
// auto bar = Bar{}; | ||
|
||
using FreeFuncPtr = double(*)(int); // a kind of type erasure | ||
FreeFuncPtr fptr = &freeFunc2; | ||
|
||
cout << (*fptr)(0) << endl; | ||
|
||
auto xyz = XYZ{}; | ||
xyz.print(); | ||
|
||
auto abc = ABC{}; | ||
abc.print(); | ||
|
||
using MemberFuncPtr = void(XYZ::*)() const; | ||
MemberFuncPtr mfptr = &XYZ::print2; // we only said a method under XYZ struct | ||
(xyz.*mfptr)(); | ||
|
||
auto lambda = []() { cout << "I am a lambda" << endl; }; | ||
lambda(); | ||
|
||
std::function<double (int)> f1; // type erasure | ||
f1 = &freeFunc1; | ||
cout << f1(100) << endl; | ||
f1 = freeFunc2; | ||
cout << f1(100) << endl; | ||
|
||
std::function<void()> f2; | ||
f2 = lambda; | ||
f2(); | ||
|
||
std::function<void(const XYZ&)> f3; | ||
f3 = &XYZ::print2; | ||
f3(xyz); | ||
|
||
// foo = bar; // strong typing does not allow this line. if duck-typed then it would be fine | ||
|
||
} |
Oops, something went wrong.