从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。
每周更新
欢迎投稿,推荐或自荐文章/软件/资源等,请提交 issue
语言律师有没有关注的
可以看这个学一点range,挺有趣的
介绍一个clang builtin扩展
#include <cstdint>
#include <cstdio>
#include <utility>
struct trade {
[[no_unique_address]] double price{42.};
[[no_unique_address]] std::size_t size{1'000};
};
int main() {
constexpr auto t = trade{};
__builtin_dump_struct(std::addressof(t), std::addressof(std::printf));
}
效果
const struct trade {
double price : 42.000000
std::size_t size : 1000
}
作者弄了个docker环境,方便c++开发 https://github.com/arnemertz/docker4c 不过一般都有自己的docker/编译机环境吧,这东西很难通用
windows平台上的内存分析方案介绍
winrt本土方案
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
int main()
{
//...
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); // This is used to auto output memory information about leaks before closing the application
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG ); // Set to output into your IDE's debug window
//...
}
//...
_CrtMemState oldState;
_CrtMemState newState;
_CrtMemState stateDiff;
_CrtMemCheckpoint(&oldState);
// ... Do some memory action
_CrtMemCheckpoint(&newState);
if (_CrtMemDifference(&stateDiff, &oldState, &newState))
{
// Simple statistics between the state
_CrtMemDumpStatistics(&stateDiff);
// Dump all created objects
_CrtMemDumpAllObjectsSince(&oldState);
// Dump the memory leaks up until now
_CrtDumpMemoryLeaks();
}
vs工具 Diagnostic Tools
MTuner
画了个图,分配内存和系统用内存比,基本一致,代码在这https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/tree/master/2021/07/29
用perf + hotspot打印火焰图,发现性能问题(其实就是perf加了个可视化gui)
用handle简单确认handle -s -p 13360
然后用Event Tracing for Windows 然后就是确认问题出在哪里然后抓WaitableEvent信息了。这个东西我不了解,不过思路有点意思
介绍counted_iterator的缺陷,简单来说 counted_iterator的逻辑是这样的
loop:
// advance
--count;
++it;
// done?
if (count != 0 && it != end) {
// read
use(*it);
goto loop;
}
这里有个问题,count是后面it判断的前提条件,这里不应该并列,假如有个无限大的view,it一时半会不会到end,这里就一直循环了
比如这样一段代码
#include <ranges>
#include <iostream>
namespace rn = std::ranges;
namespace rv = rn::views;
int main()
{
for (auto i : rv::iota(0)
| rv::filter([](auto i) { return i < 10; })
| rv::take(10))
{
std::cout << i << '\n';
}
}
这里满足不了,take(10)会永远循环,改成<10就没问题
恰好是因为这个if (count != 0 && it != end) ,it不满足条件,导致一直跑下去
所以这里的逻辑要改成
loop:
--count;
if (count != 0) {
++it; // guarded
if (it != end) {
use(*it);
goto loop;
}
}
在线点评别人的benchmark代码写的不行。没啥看的
Triton: Open-Source GPU Programming for Neural Networks 一个python调用c++库的一个神经网络库,这有个教程