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

79 draft #9

Merged
merged 4 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn
| --------------------- | --------------------- | --------------------- | --------------------- | --------------------- | --------------------- | --------------------- | --------------------- | --------------------- | --------------------- |
| [五十一](./posts/051.md) | [五十二](./posts/052.md) | [五十三](./posts/053.md) | [五十四](./posts/054.md) | [五十五](./posts/055.md) | [五十六](./posts/056.md) | [五十七](./posts/057.md) | [五十八](./posts/058.md) | [五十九](./posts/059.md) | [六十期](./posts/060.md) |
| [六十一](./posts/061.md) | [六十二](./posts/062.md) | [六十三](./posts/063.md) | [六十四](./posts/064.md) | [六十五](./posts/065.md) | [六十六](./posts/066.md) | [六十七](./posts/067.md) | [六十八](./posts/068.md) | [六十九](./posts/069.md) | [七十期](./posts/070.md) |
| [七十一](./posts/071.md) |[七十二](./posts/072.md)|[七十三](./posts/073.md)|[七十四](./posts/074.md)|[七十五](./posts/075.md)|[七十六](./posts/076.md)|[七十七](./posts/077.md)|[七十八](./posts/078.md)|||
| [七十一](./posts/071.md) |[七十二](./posts/072.md)|[七十三](./posts/073.md)|[七十四](./posts/074.md)|[七十五](./posts/075.md)|[七十六](./posts/076.md)|[七十七](./posts/077.md)|[七十八](./posts/078.md)|[七十九](./posts/079.md)||


---
18 changes: 13 additions & 5 deletions posts/078.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ title: 第78期
---
# C++ 中文周刊 第78期


从[reddit](https://www.reddit.com/r/cpp/)/[hackernews](https://news.ycombinator.com/)/[lobsters](https://lobste.rs/)/[meetingcpp](https://www.meetingcpp.com/blog/blogroll/items/Meeting-Cpp-weekly-Blogroll-344.html)摘抄一些c++动态


[周刊项目地址](https://github.com/wanghenshui/cppweeklynews)|[在线地址](https://wanghenshui.github.io/cppweeklynews/) |[知乎专栏](https://www.zhihu.com/column/jieyaren) |[腾讯云+社区](https://cloud.tencent.com/developer/column/92884)

弄了个qq频道,[手机qq点击进入](https://qun.qq.com/qqweb/qunpro/share?_wv=3&_wwv=128&inviteCode=xzjHQ&from=246610&biz=ka)
Expand All @@ -16,7 +14,7 @@ title: 第78期

可以贴在下一期草稿里 [草稿链接](https://github.com/wanghenshui/cppweeklynews/pull/9)

2020 09 02
2022 09 02

---

Expand All @@ -26,14 +24,14 @@ title: 第78期

[编译器信息最新动态推荐关注hellogcc公众号 本周更新 2022-08-31 第165期](https://github.com/hellogcc/osdt-weekly/blob/master/weekly-2022/2022-08-31.md)


## 文章

- [What is the “vector pessimization”?](https://quuxplusone.github.io/blog/2022/08/26/vector-pessimization/)

尽量让move 构造函数 noexcept, 不然用vector可能有问题,多copy

比如这个

```cpp
struct Instrument {
int n_;
Expand Down Expand Up @@ -63,6 +61,11 @@ vector本身的搬迁move的多余动作,如果能nothrow,move就更简单

free没有size看上去是个巧妙的设计,实际上隐含了挺多脏活


实际上,malloc/free/realloc等等,这些api还是太低层太精细了。对于使用者心理负担很大。

感谢@lh_mouse补充

- [C++ Coroutines: Understanding the Compiler Transform](https://lewissbaker.github.io/2022/08/27/understanding-the-compiler-transform)

协程背后都做了啥
Expand All @@ -88,13 +91,15 @@ int main(int, char** argv){
fn2(); // ignoring return value with [[nodiscard]]
}
```

- [Do we need the Y-combinator in C++? ](http://ib-krajewski.blogspot.com/2017/10/do-we-need-y-combinator-in-c-and.html?m=1)

老文,科普一下概念。

- [constexpr Function Parameters](https://www.elbeno.com/blog/?p=1685)

参数不是constexpr

```cpp
consteval auto square(int x) -> int { return x * x; }

Expand All @@ -105,7 +110,6 @@ constexpr auto twice_square(int x) -> int { return square(x); }

- [实现一个trivially_copyable的tuple ](http://www.purecpp.cn/detail?id=2309)
- [并发与异步执行流中的对象角色概述(一) ](http://www.purecpp.cn/detail?id=2310)

- [编程语言-从来如此的便是对吗?](https://zhuanlan.zhihu.com/p/558778083)

看个乐
Expand All @@ -123,6 +127,7 @@ constexpr auto twice_square(int x) -> int { return square(x); }
- [Why am I getting a null pointer crash when trying to call a method on my C++/WinRT object?](https://devblogs.microsoft.com/oldnewthing/20220901-00/?p=107097)

为什么大哥解bug这么熟练

## 视频

- [C++ Weekly - Ep 339 - `static constexpr` vs `inline constexpr`](https://www.youtube.com/watch?v=QVHwOOrSh3w)
Expand All @@ -131,6 +136,9 @@ static constexpr 和 inline constexpr区别。inline constexpr能合并文件重

聪明的你想到了static inline constexpr。这个效果就是static constexpr。static限制了范围

当然,-fmerge-contants能从另一个层面解决这个问题

感谢@lh_mouse补充

## 开源项目需要人手

Expand Down
248 changes: 248 additions & 0 deletions posts/079.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
---
layout: post
title: 第79期
---
# C++ 中文周刊 第79期


从[reddit](https://www.reddit.com/r/cpp/)/[hackernews](https://news.ycombinator.com/)/[lobsters](https://lobste.rs/)/[purecpp](http://www.purecpp.cn/)知乎等等摘抄一些c++动态


[周刊项目地址](https://github.com/wanghenshui/cppweeklynews)|[在线地址](https://wanghenshui.github.io/cppweeklynews/) |[知乎专栏](https://www.zhihu.com/column/jieyaren) |[腾讯云+社区](https://cloud.tencent.com/developer/column/92884)

弄了个qq频道,[手机qq点击进入](https://qun.qq.com/qqweb/qunpro/share?_wv=3&_wwv=128&inviteCode=xzjHQ&from=246610&biz=ka)

欢迎投稿,推荐或自荐文章/软件/资源等


可以贴在下一期草稿里 [草稿链接](https://github.com/wanghenshui/cppweeklynews/pull/10.md)

2022 0908 提前发了。周五有事
---

## 资讯

标准委员会动态/ide/编译器信息放在这里

llvm 15发布了。c++20支持等等一大堆修改

[lld 15 ELF changes ](https://maskray.me/blog/2022-09-05-lld-15-elf-changes)

[编译器信息最新动态推荐关注hellogcc公众号 本周更新 2022-09-07 第166期](https://github.com/hellogcc/osdt-weekly/blob/master/weekly-2022/2022-09-07.md)


## 文章

- [使用协程需要注意的问题(一) ](http://www.purecpp.cn/detail?id=2313)

要注意协程被线程切换影响

- [C++23特性总结 - 上](https://zhuanlan.zhihu.com/p/562383157)
- [C++23特性总结 - 下](https://zhuanlan.zhihu.com/p/562383556)

c++23详细总结

- [使用 C++ 20 协程封装 RDMA 操作](https://zhuanlan.zhihu.com/p/553140760)
- [使用 C++ 20 协程封装 UCX](https://zhuanlan.zhihu.com/p/555971343)

c++20应用协程举例

- [STL源码分析之std::function](https://zhuanlan.zhihu.com/p/560964284)
- [g++8.3,sizeof(std::function)为什么等于32](https://www.zhihu.com/question/530797704/answer/2658642917)

std::function的分析

- [用户态操作系统之四 Seastar的init函数与stop](https://zhuanlan.zhihu.com/p/561202708)
- [用户态操作系统之五 Seastar的timer](https://zhuanlan.zhihu.com/p/561214124)
- [用户态操作系统之六 Seastar的跨线程通信](https://zhuanlan.zhihu.com/p/561511141)

seastar的一些代码走读。可以看看

- [C++-模板-萃取的实现(三)](https://www.zhihu.com/column/c_1523802493980569601)

其实就是编译期检测接口的能力

- [C++ 20 协程 Coroutine(1,基础)](https://zhuanlan.zhihu.com/p/561623494)
- [C++ 20 协程 Coroutine(2,等待体)](https://zhuanlan.zhihu.com/p/561864514)
- [C++ 20 协程 Coroutine(3,剖析)](https://zhuanlan.zhihu.com/p/561884301)

一组协程教程

- [Did you know that with C++20 (constexpr containers) TMP can be achieved with STL ](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/)

```cpp
#include <ranges>

template <auto Begin, auto End, auto List>
auto slice = List
| std::ranges::views::drop(Begin)
| std::ranges::views::take(End);

static_assert(
slice<1_c, 2_c, boost::mp::list<int, double, float>()>
==
boost::mp::list<double, float>());
```

typelist实现更简单了。恐怖

- [A moved from optional](https://akrzemi1.wordpress.com/2022/09/06/a-moved-from-optional/)

optinal的move并不会真正的move T, 让T为无效value

```cpp
template <typename T>
void test(T v)
{
optional<T> o = v;
assert (o); // o contains a value
optional<T> p = std::move(o);
assert (o); // o still contains a value!
}
```
完全取决于T的move,optional会保留T的壳子。

比如unique_ptr

```cpp
int * p = new int(1);

optional<unique_ptr<int>> op = unique_ptr<int>(p);
assert (op);
assert (op->get() == p);

optional<unique_ptr<int>> o2 = std::move(op);
assert (op); // still contains a value
assert (op->get() == nullptr); // the value is moved from
assert (o2);
assert (o2->get() == p);
```

unique_ptr内部会搬走,但本身是还在optional里的。这样实现更快,对于trival类型,这样优化的可以直接memcpy,更高效一些
大概实现成这个样子

```cpp
template <typename Trivial>
class optional<Trivial>
{
bool _is_initialized;
Trivial _value;

optional() : _is_initialized(false) {}
// use defaulted copy and move
};
```

- [Using std::unique_ptr With C APIs](https://eklitzke.org/use-unique-ptr-with-c-apis)

```cpp
// Second argument is a pointer to the type of std::fclose, we could also have
// written it out explicitly as std::unique_ptr<FILE, int (*)(FILE*)>.
using FilePtr = std::unique_ptr<FILE, decltype(std::fclose) *>;

// Instantiate the FILE* with the destructor we want.
FilePtr file(std::fopen(filename, "rbe"), std::fclose);

// Do stuff with the file
std::fread(buf_.data(), 1, buf_.size(), file.get());
```

比写个deferGuard能更干净些

```cpp
using XXH3StatePtr = std::unique_ptr<XXH3_state_t, decltype(XX3_freeState) *>;
XXH3StatePtr state(XXH3_createState(), XXh3_freeState);
```
但这种写法的问题在于,需要判定创建失败/指针是否有效


- [C++23’s new function syntax](https://www.elbeno.com/blog/?p=1696)

一个简单的函数

```cpp
[[nodiscard]] auto say_a_to(
std::string_view what, std::string_view name) -> std::string {
return std::string{what} + ", " + std::string{name} + "!";
}

say_a_to("Hi", "Kate"); // -> "Hi, Kate!"

struct {
[[nodiscard]] auto operator()(
std::string_view what, std::string_view name) const -> std::string {
return std::string{what} + ", " + std::string{name} + "!";
}
} say_b_to;

say_b_to("Hello", "Tony"); // -> "Hello, Tony!"
```

没啥新奇的,但是c++23支持多维数组了,所以说`operator[]` 也算是一种函数了

比如
```cpp


struct {
[[nodiscard]] auto operator[](
std::string_view what, std::string_view name) const -> std::string {
return std::string{what} + ", " + std::string{name} + "!";
}
} say_d_to;

say_d_to["Goodbye", "Tony"]; // -> "Goodbye, Tony!"
```

```cpp
struct {
template <std::integral ...Ts>
[[nodiscard]] auto operator[](Ts... ts) const noexcept {
return (0 + ... + ts);
}
} sum;
const auto s1 = sum[1, 2, 3]; // 6
const auto s2 = sum[]; // 0
```

我只能说看个乐,别这么写求求了
## 视频

- [C++ Weekly - Ep 340 - Finally! A Simple String Split in C++! ](https://www.youtube.com/watch?v=V14xGZAyVKI)

```cpp
#include <iostream>
#include <ranges>
#include <string_view>

int main() {

// this is lazily evaluated
auto strings = std::string_view{"Hello C++ 20"} | std::views::split(' ');

// the result is a range of ranges

for (const auto &ref : strings) {
// C++ 20
std::cout << '\n' << std::string_view{ref.begin(), ref.end()};
// C++ 23
std::cout << '\n' << std::string_view{ref};
}
}
```

## 开源项目需要人手

- [asteria](https://github.com/lhmouse/asteria) 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线
- [pika](https://github.com/OpenAtomFoundation/pika) 一个nosql 存储, redis over rocksdb,非常需要人贡献代码胖友们, 感兴趣的欢迎加群294254078前来对线

## 工作招聘

寒冬了

华为出新手机了,但我不买,因为想把寒冬传给它
---

看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持!

[本文永久链接](https://wanghenshui.github.io/cppweeklynews/posts/079.html)