前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >C++ 中文周刊 2025-01-25 第176期

C++ 中文周刊 2025-01-25 第176期

作者头像
王很水
发布2025-02-04 15:39:00
发布2025-02-04 15:39:00
7700
代码可运行
举报
运行总次数:0
代码可运行

文章

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

代码语言:javascript
代码运行次数:0
复制
#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

省流,boost::synchronized_value

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,讲的是这个比玩意

代码语言:javascript
代码运行次数:0
复制
template < typename InT, typename InE, typename Function >
requiresstd::invocable< Function, std::expected< InT, InE > >
   && is_expected< typenamestd::invoke_result_t< Function, std::expected< InT, InE > > >
constexprautooperator | ( std::expected< InT, InE > && ex, Function && f ) -> typenamestd::invoke_result_t< Function, std::expected< InT, InE > >
{
returnstd::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非常容易用错

注意无符号数取反问题
代码语言:javascript
代码运行次数:0
复制
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必有问题,另外编译选项查不出来

对齐和隐式创建引发的问题
代码语言:javascript
代码运行次数:0
复制
#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 - '*'
}

看这没问题?

代码语言:javascript
代码运行次数:0
复制
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

怎么解决?复制一份

代码语言:javascript
代码运行次数:0
复制
int main() {
  Record records[] = { { 42, 42, 42}, { 42, 42, 42}  };
for (constauto& 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设置

代码语言:javascript
代码运行次数:0
复制
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}")

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-01-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CPP每周推送 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章
    • Measuring code size and performance
    • Reminder: When a C++ object fails to construct, the destructor does not run
    • Regular expressions can blow up!
    • std::nontype_t: What is it, and Why
    • Protecting Coders From Ourselves: Better Mutex Protection
    • 借助 Windsurf Sonnet Debug 过程一例
    • Parsing JSON in C & C++: Singleton Tax
    • Pipeline architectures in C++ - Boguslaw Cyganek - Meeting C++ 2024
    • C++ programmer's guide to undefined behavior: part 12 of 11
      • std::reserve和std::resize区别
      • 注意无符号数取反问题
      • 对齐和隐式创建引发的问题
    • 一个cmakelist warn设置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档