-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
最卢的瑟's homework #312
Merged
Merged
最卢的瑟's homework #312
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// loser_homework / brian.cpp | ||
// Created by brian on 2024 Apr 03. | ||
// | ||
|
||
#include <vector> | ||
#include <functional> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
template<typename T, typename Func> | ||
requires std::invocable<Func, T&> | ||
std::vector<T>& operator|(std::vector<T>& vec, Func const& func) { | ||
std::ranges::for_each(vec, func); | ||
return vec; | ||
} | ||
|
||
|
||
int main() { | ||
std::vector v{1, 2, 3}; | ||
std::function f{[](const int& i) { std::cout << i << ' '; }}; | ||
auto f2 = [](int& i) { i *= i; }; | ||
v | f2 | f; | ||
} |
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,34 @@ | ||
// | ||
// loser_homework / brian.cpp | ||
// Created by brian on 2024 Apr 03. | ||
// | ||
#include<iostream> | ||
|
||
class ComponentBase { | ||
protected: | ||
static inline std::size_t component_type_count = 0; | ||
}; | ||
|
||
template<typename T> | ||
class Component : public ComponentBase { | ||
public: | ||
static size_t component_type_id() { | ||
static size_t unique_id = component_type_count++; | ||
return unique_id; | ||
} | ||
}; | ||
|
||
class A : public Component<A> {}; | ||
|
||
class B : public Component<B> {}; | ||
|
||
class C : public Component<C> {}; | ||
|
||
int main() { | ||
std::cout << A::component_type_id() << std::endl; | ||
std::cout << B::component_type_id() << std::endl; | ||
std::cout << B::component_type_id() << std::endl; | ||
std::cout << A::component_type_id() << std::endl; | ||
std::cout << A::component_type_id() << std::endl; | ||
std::cout << C::component_type_id() << std::endl; | ||
} |
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,86 @@ | ||
// | ||
// loser_homework / scope_guard.cpp | ||
// Created by brian on 2024 Apr 03. | ||
// | ||
#include <cstdio> | ||
#include <cassert> | ||
|
||
#include <stdexcept> | ||
#include <functional> | ||
|
||
struct X { | ||
X() { puts("X()"); } | ||
|
||
X(const X&) { puts("X(const X&)"); } | ||
|
||
X(X&&) noexcept { puts("X(X&&)"); } | ||
|
||
~X() { puts("~X()"); } | ||
}; | ||
|
||
struct scope_guard { | ||
template<typename F, typename... A> | ||
requires std::invocable<F, A...> | ||
scope_guard(F&& func, A&& ...args) { | ||
gc = [&func, &args...]() mutable { | ||
/** | ||
* @brief https://en.cppreference.com/w/cpp/utility/functional/invoke | ||
* invoke: 如果\p func 是成员函数, 则\p ...args 的第一个参数是对应的对象(\p &,* ) | ||
*/ | ||
std::invoke(std::forward<F>(func), args...); | ||
}; | ||
} | ||
~scope_guard() { gc(); } | ||
scope_guard(const scope_guard&) = delete; | ||
scope_guard& operator=(const scope_guard&) = delete; | ||
|
||
private: | ||
std::function<void()> gc; | ||
}; | ||
|
||
|
||
int main() { | ||
{ | ||
// scope_guard的作用之一,是让各种C风格指针接口作为局部变量时也能得到RAII支持 | ||
// 这也是本题的基础要求 | ||
FILE* fp = nullptr; | ||
try { | ||
fp = fopen("test.txt", "a"); | ||
auto guard = scope_guard([&] { | ||
fclose(fp); | ||
fp = nullptr; | ||
}); | ||
|
||
throw std::runtime_error{"Test"}; | ||
} catch (std::exception& e) { | ||
puts(e.what()); | ||
} | ||
assert(fp == nullptr); | ||
} | ||
puts("----------"); | ||
{ | ||
// 附加要求1,支持函数对象调用 | ||
struct Test { | ||
void operator()(X* x) { | ||
delete x; | ||
} | ||
} t; | ||
auto x = new X{}; | ||
auto guard = scope_guard(t, x); | ||
} | ||
puts("----------"); | ||
{ | ||
// 附加要求2,支持成员函数和std::ref | ||
auto x = new X{}; | ||
{ | ||
struct Test { | ||
void f(X*& px) { | ||
delete px; | ||
px = nullptr; | ||
} | ||
} t; | ||
auto guard = scope_guard{&Test::f, &t, std::ref(x)}; | ||
} | ||
assert(x == nullptr); | ||
} | ||
} |
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,3 @@ | ||
为保证 atomic 对象的原子性,复制和移动都是不允许的。 | ||
C++17之前 `std::atomic<int> n = 6;` 不合法是因为 atomic 只支持列表初始化, 写成 `std::atomic<int> n = {6};` 就可以通过编译了。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 其实和这句话基本没啥关系 |
||
17+之后的 `std::atomic<int> n = 6;` 其实是原地构造,也不涉及任何复制移动,合法。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你应该聊 RVO 的事情。
这里显然是 同类型临时对象初始化,你看其他答案或者解析吧 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你
args...
不写成完美转发std::forward<A>(args)...
是为什么?