diff --git a/README.md b/README.md index 1f2531c..07a8475 100644 --- a/README.md +++ b/README.md @@ -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/058.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) | [五十九](./posts/059.md) | | diff --git a/posts/059.md b/posts/059.md new file mode 100644 index 0000000..56157fd --- /dev/null +++ b/posts/059.md @@ -0,0 +1,259 @@ +--- +layout: post +title: 第59期 +--- + +# C++ 动态新闻推送 第59期 + +从[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-326.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-20 第146期](https://github.com/hellogcc/osdt-weekly/blob/master/weekly-2022/2022-04-20.md) + +## 文章 + +- [**Did you know about C++23 proposal `Structured Bindings can introduce a Pack`?**](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/274.md) + +```c++ +#include +#include + +int main() { + auto [first, ...ts] = std::tuple{1, 2 ,3}; + assert(1 == first); +} +``` + +看个乐。提案中 + +- [The infamous bug of range-based for loops](https://www.sandordargo.com/blog/2022/04/20/range-base-p2012) + +```c++ +#include +#include +#include + +std::vector createStrings() { + return {"This", "is", "a", "vector", "of", "strings"}; +} + +int main() +{ + for (auto w: createStrings()) { + std::cout << w << " "; // this works fine + } + std::cout << std::endl; + for (auto c: createStrings()[0]) { + std::cout << c << " "; // this is UB + } + std::cout << std::endl; +} +``` + +经典bug :range for里面的变量生命周期有问题 + +聪明的你想到了用optional,照样不行 + +```c++ +#include +#include +#include +#include + +std::vector createStrings() { + return {"This", "is", "a", "vector", "of", "strings"}; +} + +std::optional> createOptionalInts() { + return std::vector{1,2,3,4,5,6}; +} + + +int main() +{ + for (auto i: createOptionalInts().value()) { + std::cout << i << " "; // UB + } + const auto v = createOptionalInts().value(); //注意,必须是值,写成const auto& 一样是UB + for (auto i: v) { + std::cout << i << " "; + } + std::cout << std::endl; +} +//0 0 27344912 0 5 6 1 2 3 4 5 6 +``` + +其实range for是语法糖,上面的代码等价于 + +```c++ +#include +#include +#include +#include + +std::optional> createOptionalInts() { + return std::optional>1; +} + +int main() +{ + auto&& range = createOptionalInts().value(); + auto position = range.begin(); + auto end = range.end(); + for(; position != end; ++position) { + std::cout << *(position) << " "; // UB + } + std::cout << std::endl; +} +``` + +问题就在这个range已经消失了,访问这个指针就有问题 + +- [How can I `co_await` on a Windows Runtime async action or operation with a timeout?](https://devblogs.microsoft.com/oldnewthing/20220415-00/?p=106486) + +有点看不懂了 + +```c++ +auto timedOut = std::make_shared(); +auto widgetOperation = GetWidgetAsync(); +auto widgetTimeout = [](auto timedOut) -> IAsyncOperation + { + co_await winrt::resume_after(15s); + *timedOut = true; + co_return nullptr; + }(timedOut); +auto widget = co_await winrt::when_any(widgetOperation, widgetTimeout); + +widgetOperation.Cancel(); +widgetTimeout.Cancel(); + +if (*timedOut) { + // timed out +} else { + // GetWidgetAsync() produced something (possibly nullptr) +} + +``` + +```c++ +auto widgetOperation = GetWidgetAsync(); +auto widgetTimeout = [] -> IAsyncOperation + { + co_await winrt::resume_after(15s); + co_return nullptr; + }(); +auto widget = co_await winrt::when_any(widgetOperation, widgetTimeout); +auto timedOut = widgetTimeout.Status() == AsyncStatus::Completed; + +widgetOperation.Cancel(); +widgetTimeout.Cancel(); + +if (timedOut) { + // timed out +} else { + // GetWidgetAsync() produced something (possibly nullptr) +} +``` + +- [Class template argument deduction may be the new hotness, but we’ll always have maker functions](https://devblogs.microsoft.com/oldnewthing/20220420-00/?p=106506) + +CTAD把活交给了编译器推导,但大家没咋用,还是有make_xx函数来构造对象,清晰,明确 + +- [Multi-GPU Programming with Standard Parallel C++, Part 1](https://developer.nvidia.com/blog/multi-gpu-programming-with-standard-parallel-c-part-1/) +- [Multi-GPU Programming with Standard Parallel C++, Part 2](https://developer.nvidia.com/blog/multi-gpu-programming-with-standard-parallel-c-part-2/) + +`nvc++ compiler`软文的感觉。介绍了一些算法可以并行,比如 + +```c++ +// Step 1: compute the number of variables contributed by every node. +int* numValuesPtr = allocateMemory(numberOfCells); +for_each(execution::par_unseq, numValuesPtr, + numValuesPtrl + numberOfCells, [=](int& numValues) +{ + int i = &numValues - numValuesPtr; + // Compute number of variables contributed by current node. + numValues = computeNumValues(i); +} ); +// 2. Compute the buffer index for every node. +int* indexPtr = allocateMemory(numberOfCells); +exclusive_scan(execution::par_unseq, numValuesPtr, + numValuesPtr + numberOfCells, indexPtr, 0); +// 3. Pack the data into the buffer. +for_each(execution::par_unseq, indexPtr, + indexPtr + numberOfCells, [=](int& index) +{ + int i = &index - indexPtr; + packCellData(i, index); +} ); +``` + + + +- [Faster GDB Startup](https://tromey.com/blog/?p=1084) + +介绍gdb启动都做了什么以及如何优化启动速度 + +- [What’s new for C++ Debugging in Visual Studio Code](https://devblogs.microsoft.com/cppblog/whats-new-for-c-debugging-in-visual-studio-code/?WT.mc_id=academic-0000-abartolo) + +介绍vscode更新的调试功能(谁用vscode调试啊) + +## 视频 + +- [C++ Weekly - Ep 320 - Using `inline namespace` To Save Your ABI ](https://www.youtube.com/watch?v=rUESOjhvLw0) + +```c++ +namespace tool { +inline namespace v1_0_0 { +struct Data { + int a; + bool b; + std::string c; +}; +bool foo(const Data d); +} +} + +int main() { + const tool::Data d; + return tool::foo(d); +} +``` + +看到这个用法,inline namespace在调用的时候可以省掉,但是这个inline namespace的符号可以保证唯一,这样就避免了不同版本造成的ABI break + +很妙,但没人用。这属于项目管理的一部分。严格来说很难出现ABI break + + + +## 开源项目需要人手 + +- [asteria](https://github.com/lhmouse/asteria) 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线 +- [pika](https://github.com/OpenAtomFoundation/pika) 一个nosql 存储, redis over rocksdb,非常需要人贡献代码胖友们, 感兴趣的欢迎加群294254078前来对线 + +## 新项目介绍/版本更新 + +- [mleak](https://github.com/mini-rose/mleak) 劫持malloc/free 分析内存泄漏 +- [boostdep-report](https://pdimov.github.io/boostdep-report/) 分析boost各个组件的依赖关系 比如asio依赖boost core之类的 +- [vcpkg April 2022 Release is Now Available](https://devblogs.microsoft.com/cppblog/vcpkg-april-2022-release-is-now-available/) +- [poco Release 1.11.2 Available](https://pocoproject.org/blog/?p=1156) + +## 工作招聘 + +互联网寒冬了胖友们 + +--- + +看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持! + +[本文永久链接](https://wanghenshui.github.io/cppweeklynews/posts/059.html)