From 7b4547f8abdcb97354b8f6a11a577b7e25ad28ca Mon Sep 17 00:00:00 2001 From: Ted Mosly Date: Tue, 26 Jul 2022 21:47:48 +0800 Subject: [PATCH] 72 --- README.md | 2 +- posts/072.md | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 posts/072.md diff --git a/README.md b/README.md index 9a7248e..501815f 100644 --- a/README.md +++ b/README.md @@ -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/071.md) |[七十二](./posts/072.md)||||||||| --- diff --git a/posts/072.md b/posts/072.md new file mode 100644 index 0000000..10d235a --- /dev/null +++ b/posts/072.md @@ -0,0 +1,365 @@ +--- +layout: post +title: 第72期 +--- +# C++ 中文周刊 第72期 + +从[reddit](https://www.reddit.com/r/cpp/)/[hackernews](https://news.ycombinator.com/)/[lobsters](https://lobste.rs/)/摘抄一些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) + +2022 0726 最近有点忙 + +--- + +## 资讯 + +标准委员会动态/ide/编译器信息放在这里 +[七月邮件汇总](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/#mailing2022-07) + +[编译器信息最新动态推荐关注hellogcc公众号 本周更新 2022-07-20 第159期](https://github.com/hellogcc/osdt-weekly/blob/master/weekly-2022/2022-07-20.md) + + + +## 文章 + +- [趣谈哈希表优化:从规避 Hash 冲突到利⽤ Hash 冲突](https://mp.weixin.qq.com/s/oeuExiW3DYQnBG8HvDgBAg) + +讲完美哈希表的。这个知识好像没怎么普及开,这里推荐一下 + +- [Making sure that people use make_unique and make_shared to make your object](https://devblogs.microsoft.com/oldnewthing/20220721-00/?p=106879) + +一个思路,禁止各种make,只能从静态函数构造 + +```cpp +template +struct require_make_shared : + public std::enable_shared_from_this +{ +protected: + struct use_the_create_method { + explicit use_the_create_method() = default; + }; + +public: + template + static auto create(Args&&... args) + { + return std::make_shared + (use_the_create_method{}, + std::forward(args)...); + } + + // Deny copy construction + require_make_shared(require_make_shared const&) = delete; +}; + +class Widget : public require_make_shared +{ +public: + Widget(use_the_create_method); + Widget(use_the_create_method, int); + Widget(use_the_create_method, int, int); +}; + +void test() +{ + auto v1 = Widget::create(); // okay + auto v2 = std::make_shared(); // nope + auto v3 = std::make_unique(); // nope + Widget v4; // nope +} +``` + +- [6 C++23 features improving string and string_view](https://www.sandordargo.com/blog/2022/07/20/6-features-improving-string-string_view-in-cpp23) + +```cpp +std::string s{"there is a needle in the haystack"}; +std::string_view sv{"acdef"}; + +if (s.contains("needle")) { // 总算有这个api了 + std::cout << "we found a needle in: " << std::quoted(s) << '\n'; +} + +if (!sv.contains('b')) { + std::cout << "we did not find a 'b' in: " << std::quoted(sv) << '\n'; +} + +std::string s(nullptr); //不会挂了 +``` + +basic_string::resize_and_overwrite() 可以省一个写,用不上的可以先用gcc内置函数 + +```cpp +std::string s { "Food: " }; +s.resize_and_overwrite(10, [](char* buf, int n) { + return std::find(buf, buf + n, ':') - buf; +}); +std::cout << "2. " << std::quoted(s) << '\n'; +``` + +- [finally. #embed](https://thephd.dev/finally-embed-in-c23) + +c23支持embed了,c++23还不支持std::embed。服了 + +介绍一下embed,看代码你就懂了 + +```c +#include + +int main (int, char*[]) { + static const char sound_signature[] = { +#embed + }; + static_assert((sizeof(sound_signature) / sizeof(*sound_signature)) >= 4, + "There should be at least 4 elements in this array."); + + // verify PCM WAV resource signature (at run-time) + assert(sound_signature[0] == 'R'); + assert(sound_signature[1] == 'I'); + assert(sound_signature[2] == 'F'); + assert(sound_signature[3] == 'F'); + + return 0; +} + +``` + +- [Custom types and std::format from C++20 ](https://www.cppstories.com/2022/custom-stdformat-cpp20/) + +定制formatter,代码不贴了 + +- [Did you know that C++23 added auto(x): decay-copy in the language?](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/287.md) + +```cpp +auto foo(auto&&); + +int main() { + int i{}; + const int c{}; + + foo(42); // int + foo(i); // int& + foo(int{i}); // int + foo(auto{i}); // int + foo(auto(42)); // int + foo(c); // const int& + foo(int{c}); // int + foo(auto(c)); // int +} +``` + +这个也讲过很多次了。有些场景是需要copy语义的,否则莫名其妙出现了改动,解决办法就是让他退化,拿到copy + +这个例子也说过很多次了 + +```cpp +#include + +template +constexpr std::decay_t +decay_copy(T&& v) + noexcept(std::is_nothrow_convertible_v>) // exposition only +{ + return std::forward(v); +} + +int main() { + std::vector v1{1, 2, 1, 1, 1}; + auto v2 = v1; + v1.erase( + std::remove(begin(v1), end(v1), *std::max_element(begin(v1), end(v1))), + v1.end()); + v2.erase(std::remove( + begin(v2), end(v2), + decay_copy(*std::max_element(begin(v2), end(v2)))), + v2.end()); + std::cout << "v1.size()=" << v1.size() << std::endl + << "v2.size()=" << v2.size() + << std::endl; + return 0; +} +``` + +- [Upside Down Polymorphic Inheritance Leveraging P2162 for Fun & Profit](https://blog.nickelp.ro/posts/p2162/) + +p2162继承variant各个编译器已经支持了。作者展示了一种优雅的用法 + +```cpp +#include +#include + +using namespace std; + +struct Request { + static constexpr uint8_t id_num {1}; + uint8_t req_val; + + void encode(ostream& os) const { + os.put(req_val); + } + + void decode(istream& is) { + req_val = is.get(); + } +}; + +struct Affirmative { + static constexpr uint8_t id_num {2}; + uint8_t resp_val; + + void encode(ostream& os) const { + os.put(resp_val); + } + + void decode(istream& is) { + resp_val = is.get(); + } +}; + +struct Failure { + static constexpr uint8_t id_num {3}; + uint8_t resp_val; + + void encode(ostream& os) const { + os.put(resp_val); + } + + void decode(istream& is) { + resp_val = is.get(); + } +}; + +#include +#include + +struct Packet : variant { + using variant::variant; + + Packet(uint8_t id) : variant {from_id(id)} {} + + Packet(istream& is) { + decode(is); + } + + void encode(ostream& os) const { + visit([&](const auto& inner){ + os.put(inner.id_num); + inner.encode(os); + }, *this); + } + + void decode(istream& is) { + *this = from_id(is.get()); + visit([&](auto& inner){ inner.decode(is); }, *this); + } + +private: + static Packet from_id(uint8_t id) { + switch(id) { + case Request::id_num: + return Request {}; + case Affirmative::id_num: + return Affirmative {}; + case Failure::id_num: + return Failure {}; + } + throw std::runtime_error {"Invalid Packet Id!"}; + } +}; + +#include +#include + +#include +using fmt::print; + +int main(void) { + Packet p {Request {.req_val {5}}}; + stringstream buf; + + p.encode(buf); + + Packet decoded {buf}; + visit([](const auto& inner){ + using T = decltype(auto(inner)); + if constexpr(same_as) + print("I am a Request with a val of: {}", inner.req_val); + else if constexpr(same_as) + print("I am an Affirmative with a val of: {}", inner.resp_val); + else if constexpr(same_as) + print("I am a Failure with a val of: {}", inner.resp_val); + }, decoded); +} +``` + +这种写法确实挺爽的,有点猥琐的模式匹配那个味了,https://godbolt.org/z/rqYeY5r1f + +作者也展示了用concept达到类似的效果 +```cpp +#include + +struct Stranger {}; + +using Person = variant; + +template +concept IsNamed = requires(T p) { + { p.name() } -> convertible_to; +}; + +void print_name(Person& p) { + visit([](auto& inner) { + if constexpr(IsNamed) + print("Name: {}\n", inner.name()); + else + print("This is a stranger!\n"); + }, p); +} +``` + +也不是不行,就是concept一般人比较不好接受 + +- [Size Optimization Tricks](https://justine.lol/sizetricks/) + +讲裁剪二进制的。 + +## 视频 + +- [C++ Weekly - Ep 334 - How to Put a Lambda in a Container ](https://www.youtube.com/watch?v=qmd_yxSOsAE) + +通过函数make_lambda返回一个lambda,然后推导这个lambda的类型,就可以放到vector里了。直接用lambda不行,类型信息是匿名的,无法使用。 +或者用std::function不就行了? + +## 开源项目需要人手 + +- [asteria](https://github.com/lhmouse/asteria) 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线 +- [pika](https://github.com/OpenAtomFoundation/pika) 一个nosql 存储, redis over rocksdb,非常需要人贡献代码胖友们, 感兴趣的欢迎加群294254078前来对线 + +## 新项目介绍/版本更新 + +- [在线配置属于你自己的clang-format](https://clang-format-configurator.site/) +- [carbon-lang号称做下一个c++](https://github.com/carbon-language/carbon-lang) + +说实话格局低了。自己开新坑不如隔壁meta推出新IR修缮 +另外下一个c++不是rust么,都这么多年了,为啥会选你carbon,除非像go一样硬推 + +- [indirect_value](https://github.com/jbcoe/indirect_value) pimpl惯用法的另一种实现, 可以看这个设计https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1950r1.html 挺有意思的 +- [BTree](https://github.com/frozenca/BTree/blob/main/fc_btree.h) 一个可罗盘的btree实现。看个乐 + + +## 工作招聘 + +有没有需要招扫地僧式保洁的。我会拖地 + +--- + +看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持! + +[本文永久链接](https://wanghenshui.github.io/cppweeklynews/posts/072.html)