Skip to content
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 7 commits into from
Apr 4, 2024
Merged

最卢的瑟's homework #312

merged 7 commits into from
Apr 4, 2024

Conversation

Corgile
Copy link
Contributor

@Corgile Corgile commented Apr 4, 2024

请国王查收

Copy link
Collaborator

@rsp4jack rsp4jack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI 没过。

.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
@Mq-b Mq-b added the category: homework 卢瑟作业 label Apr 4, 2024
* @brief https://en.cppreference.com/w/cpp/utility/functional/invoke
* invoke: 如果\p func 是成员函数, 则\p ...args 的第一个参数是对应的对象(\p &,* )
*/
std::invoke(std::forward<F>(func), args...);
Copy link
Owner

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)... 是为什么?

@@ -0,0 +1,3 @@
为保证 atomic 对象的原子性,复制和移动都是不允许的。
C++17之前 `std::atomic<int> n = 6;` 不合法是因为 atomic 只支持列表初始化, 写成 `std::atomic<int> n = {6};` 就可以通过编译了。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实和这句话基本没啥关系

@@ -0,0 +1,3 @@
为保证 atomic 对象的原子性,复制和移动都是不允许的。
C++17之前 `std::atomic<int> n = 6;` 不合法是因为 atomic 只支持列表初始化, 写成 `std::atomic<int> n = {6};` 就可以通过编译了。
17+之后的 `std::atomic<int> n = 6;` 其实是原地构造,也不涉及任何复制移动,合法。
Copy link
Owner

@Mq-b Mq-b Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你应该聊 RVO 的事情。
C++17 RVO 只有两种情况:

  1. 同类型临时对象初始化
  2. return 临时量

这里显然是 同类型临时对象初始化,你看其他答案或者解析吧

Copy link
Owner

@Mq-b Mq-b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如上

@Corgile Corgile requested a review from Mq-b April 4, 2024 08:50
在C++17及之后 `std::atomic<int> n = 6;` 可以通过编译,主要是因为C++17引入了对复制消除的强制要求,
允许从基础类型直接初始化 `std::atomic` 对象, 即使 `std::atomic<int>` 的复制构造函数被删除。

在C++17之前, 这种初始化因为尝试调用被删除的复制构造函数而失败。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是不对,你看标准答案去吧。

允许从基础类型直接初始化 `std::atomic` 对象, 即使 `std::atomic<int>` 的复制构造函数被删除。

在C++17之前, 这种初始化因为尝试调用被删除的复制构造函数而失败。
这个例子其实展示的是C++17改进了直接初始化和复制消除的规则,而不是传统意义上的RVO。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与初始化本身没啥关系

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的👌 国王万岁

@rsp4jack
Copy link
Collaborator

rsp4jack commented Apr 4, 2024

@Mq-b 你这 review 的也太难受了,一个 review 里可以有很多个 comment,你非要每个都 comment 一次。

@rsp4jack
Copy link
Collaborator

rsp4jack commented Apr 4, 2024

@Mq-b 另外你如果要合并,一定要用 Squash and merge(那个又大又绿的合并按钮右侧有个倒三角,点它就能改 method)。这么一条 commit tree 如果进了仓库,那就是在糟蹋提交记录。

@Mq-b Mq-b merged commit 809b0b4 into Mq-b:main Apr 4, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants