Skip to content

Commit

Permalink
58
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghenshui committed Apr 15, 2022
1 parent 349e205 commit e66448e
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn
| [二十一](./posts/021.md) | [二十二](./posts/022.md) | [二十三](./posts/023.md) | [二十四](./posts/024.md) | [二十五](./posts/025.md) | [二十六](./posts/026.md) | [二十七](./posts/027.md) | [二十八](./posts/028.md) | [二十九](./posts/029.md) | [三十期](./posts/030.md) |
| [三十一](./posts/031.md) | [三十二](./posts/032.md) | [三十三](./posts/033.md) | [三十四](./posts/034.md) | [三十五](./posts/035.md) | [三十六](./posts/036.md) | [三十七](./posts/037.md) | [三十八](./posts/038.md) | [三十九](./posts/039.md) | [四十期](./posts/040.md) |
| [四十一](./posts/041.md) | [四十二](./posts/042.md) | [四十三](./posts/043.md) | [四十四](./posts/044.md) | [四十五](./posts/045.md) | [四十六](./posts/046.md) | [四十七](./posts/047.md) | [四十八](./posts/048.md) | [四十九](./posts/049.md) | [五十期](./posts/050.md) |
| [五十一](./posts/051.md) | [五十二](./posts/052.md) | [五十三](./posts/053.md) | [五十四](./posts/054.md)| [五十五](./posts/055.md) | [五十六](./posts/056.md) | [五十七](./posts/057.md) | | | |
| [五十一](./posts/051.md) | [五十二](./posts/052.md) | [五十三](./posts/053.md) | [五十四](./posts/054.md)| [五十五](./posts/055.md) | [五十六](./posts/056.md) | [五十七](./posts/057.md) | [五十八](./posts/058.md) | | |



Expand Down
7 changes: 4 additions & 3 deletions posts/054.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ for(auto t : z1)
- [overload 167](https://accu.org/journals/overload/30/167/overload167.pdf)
这是个期刊,每期都会录入几篇文章,文章很长。内容标题是
这是个期刊,每期都会录入几篇文章,文章很长。内容标题是
- What are you optimizing for? 没啥说的
- Revisiting Data-Oriented Design
- An Associative Container for Non-bash Shell Scripts
- C++20 Benefits: Consistency With Ranges
没啥值得说的
- [PartialExecuter: Reducing WebAssembly size by exploring all executions in LLVM](https://medium.com/leaningtech/partialexecuter-reducing-webassembly-size-by-exploring-all-executions-in-llvm-f1ee295e8ba)
Expand Down Expand Up @@ -112,7 +113,7 @@ for(auto t : z1)
嵌入式场景编译器优化减小程序体积
体积影响分发。从二进制角度观测/评估哪里可以省掉,用[bloaty](https://github.com/google/bloaty)查
比如strip 减少内联 (`__attribute__((noinline))`)/模板实例化以及二进制工具修改
编译选项,-Os -flto -Wl,--strip-all (不要-g) -fno-unroll-loops -fno-execption -fno-rtti
编译选项,-Os -flto -Wl,--strip-all (不要-g) -fno-unroll-loops -fno-exception -fno-rtti
以及编译器本身提供的减少体积的选项,比如thin-lto(llvm) -fmerge-all-constant -mllvm hot-cold-split
代码组织上,不要继承不要类不要lambda,不要复制,实现移出头文件,数据结构简单就好,算法用简单的,不需要考虑性能问题,够用,拷贝尽量用memcpy
Expand All @@ -125,7 +126,7 @@ for(auto t : z1)
- 推荐观看 [Embracing `noexcept` Operators and Specifiers Safely - John Lakos - CppCon 2021](https://www.youtube.com/watch?v=3GwNjGMKBtI)
如果一个类只声明不实现,不能保证成员函数是noexpect(true)的(析构函数除外),之前咱们也聊过zero cost execption相关
如果一个类只声明不实现,不能保证成员函数是noexpect(true)的(析构函数除外),之前咱们也聊过zero cost exception相关
这个视频讨论了noexcept的方方面面。比较深入。值得一看。
- [Embracing (and also Destroying) Variant Types Safely - Andrei Alexandrescu - CppCon 2021](https://www.youtube.com/watch?v=va9I2qivBOA&t=94s)
Expand Down
2 changes: 1 addition & 1 deletion posts/057.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ xmake真的挺好用的。自己构建小东西,推荐使用

- [Fuzzy search for C++ Reference, Qt documentation and more from shell, Vim or Neovim](https://cukic.co/2022/04/02/fuzzy-search-documentation/)

主要是使用zeal的cli工具嵌入,最近才知道zeal支持linux了。可以下载cpp的文档然后用zeal-cli来搜索,省一个搜索的功夫
主要是使用zeal的cli工具嵌入,最近才知道zeal支持linux了。可以下载cpp的文档然后用[zeal-cli](https://gitlab.com/ivan-cukic/zeal-lynx-cli)来搜索,省一个搜索的功夫

- [Design Patterns VS Design Principles: Abstract Factory](https://www.fluentcpp.com/2022/04/06/design-patterns-vs-design-principles-abstract-factory/)

Expand Down
312 changes: 312 additions & 0 deletions posts/058.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
---
layout: post
title: 第58期
---

# C++ 动态新闻推送 第58期



[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-Blogroll-325.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)

欢迎投稿,推荐或自荐文章/软件/资源等,请[提交 issue](https://github.com/wanghenshui/cppweeklynews/issues)

你们说整个代码走读周会/项目分享 靠谱么。我感觉拉不到人

---

## 资讯

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

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

## 文章

- [**Did you know that concept can be passed via lambda expression** ](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/273.md/)

```c++
static_assert([]<class T>{ return std::integral<T>; }.operator()<int>());

struct f { auto foo() -> void; };
static_assert([](auto t){ return requires { t.foo(); }; }(f{}));
```
我觉得这玩意还是不要知道的好
- [可怕!CPU暗藏了这些未公开的指令!](https://zhuanlan.zhihu.com/p/498014297)
看个乐,挺有意思的
- [Pointers Are Complicated III, or: Pointer-integer casts exposed](https://www.ralfj.de/blog/2022/04/11/provenance-exposed.html)
一些c指针的缺陷。老生常谈了属于是
- [Using std::chrono](https://akrzemi1.wordpress.com/2022/04/11/using-stdchrono/)
复习一下chrono,c++20有个[file_clock](https://zh.cppreference.com/w/cpp/chrono/file_clock),干嘛的
- [SnapdragonProfiler崩溃问题分析](https://zhuanlan.zhihu.com/p/498034539)
windbg调试手把手教学
- [Three Benchmarks of C++20 Ranges vs Standard Algorithms ](https://www.cppstories.com/2022/ranges-perf/)
测了几种算法range和标准实现的表现。range下限稳定。能用range尽量用range
- [Please repeat yourself: The `noexcept(noexcept(…))` idiom](https://devblogs.microsoft.com/oldnewthing/20220408-00/?p=106438)
```c++
template<typename T>
struct Holder
{
T value;
template<typename... Args>
Holder(Args&&... args) :
value(std::forward<Args>(args)...) {}
};
template<typename U> Holder(U&&) ->
Holder<std::remove_reference_t<U>>
```

省一个类型缩写 `Holder(42)`而不是`Holder<int>42` 但是问题来了,如果T的构造抛异常就完了

标记noexcept,怎么标?

```c++
template<typename... Args>
Holder(Args&&... args)
noexcept(noexcept(T(std::forward<Args>(args)...))) :
value(std::forward<Args>(args)...) {}
```
- [Type Erasure](http://www.modernescpp.com/index.php/type-erasure)
```c++
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Object { // (2)
public:
template <typename T> // (3)
Object(T&& obj): object(std::make_shared<Model<T>>(std::forward<T>(obj))){}
std::string getName() const { // (4)
return object->getName();
}
struct Concept { // (5)
virtual ~Concept() {}
virtual std::string getName() const = 0;
};
template< typename T > // (6)
struct Model : Concept {
Model(const T& t) : object(t) {}
std::string getName() const override {
return object.getName();
}
private:
T object;
};
std::shared_ptr<const Concept> object;
};
void printName(std::vector<Object> vec){ // (7)
for (auto v: vec) std::cout << v.getName() << '\n';
}
struct Bar{
std::string getName() const { // (8)
return "Bar";
}
};
struct Foo{
std::string getName() const { // (8)
return "Foo";
}
};
int main(){
std::vector<Object> vec{Object(Foo()), Object(Bar())}; // (1)
printName(vec);
std::cout << '\n';
}
```

学会这种封装思想.虽然不太会用到

- [Memory-Size Literals](https://wunkolo.github.io/post/2022/02/memory-size-literals/)

```c++
constexpr unsigned long long operator""_KiB(unsigned long long int x) {
return 1024ULL * x;
}

constexpr unsigned long long operator""_MiB(unsigned long long int x) {
return 1024_KiB * x;
}

constexpr unsigned long long operator""_GiB(unsigned long long int x) {
return 1024_MiB * x;
}

constexpr unsigned long long operator""_TiB(unsigned long long int x) {
return 1024_GiB * x;
}

constexpr unsigned long long operator""_PiB(unsigned long long int x) {
return 1024_TiB * x;
}
```

就是这段代码,分享给大家,增加代码可读性

作者拿这段代码到处提MR ,比如这个https://github.com/xenia-project/xenia/pull/1935/

- [Stringy Templates](https://vector-of-bool.github.io/2021/10/22/string-templates.html)

还是fix_string

```c++
template <size_t Length>
struct fixed_string {
char _chars[Length+1] = {}; // +1 for null terminator
};
template <size_t N>
fixed_string(const char (&arr)[N])
-> fixed_string<N-1>; // Drop the null terminator

template <fixed_string<6> Str>
struct foo;

foo<"Hello!"> hello;
foo<"world!"> world;
foo<"nope"> b; // FAIL!

```
foo的这个6非常碍眼且不合理。c++20,可以自动推导了
```c++
template <fixed_string S>
struct ctad_foo {};
ctad_foo<"Hello"> h
ctad_foo<"user"> u;
```

更离谱的

```c++
template <fixed_string> // [1]
struct named_type {};

template <> // [2]
struct named_type<"integer"> { using type = int; };

template <> // [2]
struct named_type<"boolean"> { using type = bool; };

template <fixed_string S> // [3]
using named_type_t = named_type<S>::type;

named_type_t<"integer"> v = 42;
named_type_t<"boolean"> b = false;



template <fixed_string Name>
concept names_a_type = requires {
// Require that `named_type_t<Name>` produces a valid type
typename named_type_t<Name>;
};

static_assert(names_a_type<"integer">);
static_assert(!names_a_type<"widget">);

template <fixed_string S>
auto do_something() {
static_assert(
names_a_type<S>,
"The given string must name a registered type!");
}

```
类型信息真正的存下来了。不过一时半会用不上
concept+ lambda
```c++
#include <concepts>
template <auto Constraint> struct requires_ {
template <class T> requires (Constraint.template operator()<T>()) operator T();
};
#define $requires(...) requires_<[]<class _>{ return __VA_ARGS__; }>{}
template<class T>
concept fooable = requires(T t) {
t.foo(
$requires(std::integral<_>),
$requires(std::same_as<short, _>)
);
};
struct bar { void foo(); };
static_assert(not fooable<bar>);
struct foo1 { void foo(int, short); };
static_assert(fooable<foo1>);
struct foo2 { void foo(int, auto); };
static_assert(fooable<foo2>);
```



## 视频

- [C++ Weekly - Ep 319 - A JSON To C++ Converter ](https://www.youtube.com/watch?v=HROQPE59q_w)

一个编译期的json parser。代码在这里https://github.com/lefticus/json2cpp 玩出花来了





## 开源项目需要人手

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

## 新项目介绍/版本更新

- [C++20 library for comfortable and efficient dynamic polymorphism ](https://github.com/kelbon/AnyAny)
- [seer](https://github.com/epasveer/seer) 一个gdb前端
- [perf-ninja](https://github.com/dendibakh/perf-ninja) 一个c++实验课程,推荐大家都做一做,他的书这里可以领https://book.easyperf.net/perf_book
- [boost 179](https://www.boost.org/users/history/version_1_79_0.html) 一些fix。没有新库,都是fix



---

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

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

0 comments on commit e66448e

Please sign in to comment.