C++ 中文周刊 2025-01-25 第176期
公众号
点击「查看原文」跳转到 GitHub 上对应文件,链接就可以点击了
qq群 点击进入 满了加这个 729240657
欢迎投稿,推荐或自荐文章/软件/资源等,评论区留言
资讯
标准委员会动态/ide/编译器信息放在这里 一月邮件
安全问题还在吵架
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2025-01-08 第288期
文章
Measuring code size and performance
他的场景 不用异常处理错误,还是快的。虽然这些年来异常已经进化了快了一些
Reminder: When a C++ object fails to construct, the destructor does not run
注意你手写的guard类 构造函数不能失败,否则构造失败不析构就泄漏了
Regular expressions can blow up!
省流,fuck std::regex
#include <iostream>
#include <regex>
int main() {
std::string text = "Everyone loves Lucy.";
std::regex pattern(R"(.*+s}}@w)");
// Perform regex search
std::smatch match;
bool found = std::regex_search(text, match, pattern);
std::cout << "Regex search result: "
<< (found ? "Match found" : "No match") << std::endl;
return 0;
}
这段代码运行七分钟
std::nontype_t: What is it, and Why
抽象type 一个tag 重载 帮助function ref匹配constexpr函数
Protecting Coders From Ourselves: Better Mutex Protection
folly也有synchronizedwith<T>
感觉这个周刊写的越多越发现重复。。评论区不知道的说一下,不行就folly组件挨个介绍
借助 Windsurf Sonnet Debug 过程一例
虽然和c++没啥关系,分享只是感叹AI太强了,程序员真的有点可有可无了
Parsing JSON in C & C++: Singleton Tax
省流 池化加速助力 解析加快 局部性功劳
Pipeline architectures in C++ - Boguslaw Cyganek - Meeting C++ 2024
他讲的不是CPU那个pipeline,也不是任务调度那个pipeline,讲的是这个比玩意
template < typename InT, typename InE, typename Function >
requires std::invocable< Function, std::expected< InT, InE > >
&& is_expected< typename std::invoke_result_t< Function, std::expected< InT, InE > > >
constexpr auto operator | ( std::expected< InT, InE > && ex, Function && f ) -> typename std::invoke_result_t< Function, std::expected< InT, InE > >
{
return std::invoke( std::forward< Function >( f ), /***/ std::forward< std::expected< InT, InE > >( ex ) );
}
....
// ----------------------------------------------------------------------------------------------------------------
// Here we create our CUSTOM PIPE
auto res = PayloadOrError { Payload { "Start string ", 42 } } | Payload_Proc_1 | Payload_Proc_2 | Payload_Proc_3 ;
// ----------------------------------------------------------------------------------------------------------------
就是类似range的pipe语法传播
如何评价,光顾着耍帅了有点
C++ programmer's guide to undefined behavior: part 12 of 11
继续介绍坑点
std::reserve和std::resize区别
resize会初始化0, size == capacity
reserve不会 size == 0
但没有resize_with_overwrite这种跳过填0的操作,只有string有
resize reserve非常容易用错
注意无符号数取反问题
struct Element {
size_t width; // original non-scaled width
....
};
// You are using smart component system that uses
// IDs to refer to elements.
using ElementID = uint64_t;
// Positions in OpenGL/DirectX/Vulkan worlds are floats
struct Offset {
float x;
float y;
};
size_t get_width(ElementID);
float screen_scale();
void move_by(ElementID, Offset);
void on_unchecked(ElementID el) {
auto w = get_width(el);
move_by(el, Offset {
-w * screen_scale() * 0.3f,
0.0f
});
}
这个-w必有问题,另外编译选项查不出来
对齐和隐式创建引发的问题
#pragma pack(1)
struct Record {
long value;
int data;
char status;
};
int main() {
Record r { 42, 42, 42};
static_assert(sizeof(r) == sizeof(int) + sizeof(char) + sizeof(long));
std::cout <<
std::format("{} {} {}", r.data, r.status, r.value); // 42 - '*'
}
看这没问题?
int main() {
Record records[] = { { 42, 42, 42}, { 42, 42, 42} };
static_assert(sizeof(records) ==
2 * ( sizeof(int) + sizeof(char) + sizeof(long) ));
for (const auto& r: records) {
std::cout << std::format("{} {} {}", r.data, r.status, r.value); // 42 - '*'
}
}
问题来自赋值隐式创建int 但实际上不是int,是pack bit
怎么解决?复制一份
int main() {
Record records[] = { { 42, 42, 42}, { 42, 42, 42} };
for (const auto& r: records) {
// C++23 has wonderful auto() for this purpose
std::cout << std::format("{} {} {}",
auto(r.data), auto(r.status), auto(r.value));
// In C++20,
auto data = r.data; auto status = r.status; auto value = r.value;
std::cout << std::format("{} {} {}", data, status, value);
// Or completely ugly and unstable to type changes
std::cout << std::format("{} {} {}", static_cast<int>(r.data),
static_cast<char>(r.status),
static_cast<long>(r.value>));
}
}
但实际上编译器应该阻止这种行为
后面还有coroutine的问题,特长。我准备拆开单独写一下
一个cmakelist warn设置
set (MY_CXX_FLAGS
"-Wall \
-Wextra \
-Werror \
-Wsuggest-override \
-Wno-unknown-warning-option \
-Wno-array-bounds \
-pedantic-errors" )
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_CXX_FLAGS}")
开源项目介绍
- asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群753302367和作者对线
- https://github.com/msqr1/importizer 帮助把include转成module 点子有意思
- https://github.com/meow-watermelon/puppy-eye 一个监控命令行,有点意思