C++ 中文周刊 第96期

周刊项目地址

公众号

RSS https://github.com/wanghenshui/cppweeklynews/releases.atom

欢迎投稿,推荐或自荐文章/软件/资源等

提交 issue

0113

部门聚餐了延误了一下。本周没看视频


资讯

标准委员会动态/ide/编译器信息放在这里

编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-01-11 第184期

一个博客收集网站 https://swedencpp.se/blogs 英文c++的最新博客收集

文章

取对数

哎。倒腾cmake我头疼

有点意思。这种针对牛逼网卡的优化我感觉大厂都在做类似的玩意

又一个c++项目引入rust的。之前有火狐浏览器,clickhouse, redpanda,linux内核,唱衰一波,不过不要慌,c++程序员一时半会不会失业

看代码

constexpr auto get = [](auto value) {
  if consteval {
    return value;
  } else {
    auto result = 0;
    asm("movl $42, %%eax\n" : "=r" (result) );
    return result;
  }
};

static_assert(0 == get(0));
static_assert(4 == get(4));
static_assert(2 == get(2));

consteval auto fn() {
    return get(0);
}

int main(int argc, char**) {
  assert(42 == get(0));
  assert(42 == get(argc));
  return fn();
}

希望大家身体健康,活得久一点,就能用到静态反射了

编译器标准库用到很多变量是两个下划线 下划线大写字母开头的。自己定义变量尽量别用

测了一下simdutdf在Zen4的表现,挺强的

blake3是不是就是simd加速的?

给oilshell设计的GC。没仔细看

optional做函数参数是十分不恰当的。这玩意只适合做返回值

QVarLengthArray类似vector,区别在于对小数据做SBO优化,且resize不会做额外的初始化动作。初始化动作是c++默认有的。很多场景来看是多余的,比如string。c++20/23做了许多修正

比如

std::unique_ptr<int[]> p3 = std::make_unique_for_overwrite<int[]>(100'000);

再比如string

// C++23
std::string s = ~~~;
 
auto oldSize = s.size();
 
s.resize_and_overwrite(100'000, [oldSize](char *buf, std::size_t count) {
 
  // For starters, s will *reserve* enough space, without initializing it.
  //
  // - buf points to the string's storage (i.e. s.data()) *after* the reserve;
  // - count is the 1st argument to resize_and_overwrite (100k), so
  //   we can re-use this function with different `count`s.
 
 
  // Populate the range [buf, buf+count]. We can mutate the entirety of
  // the string's buffer. But let's say we're just interested in populating
  // the new contents -- from position oldSize up to count.
  for (size_it i = oldSize; i < count; ++i)
    buf[i] = generateData(i);
 
  // Notes:
  // - If we're growing, the newly created storage is *uninitialized*.
  //   Don't read from it!
  //
  // - The old contents are still there, and we can access them freely.
  //   If needed, carry `oldSize` manually, to identify where to start 
  //   writing (and leave the old contents alone).
  //
  // - It is legal to write into buf[count],
  //   but it will be overwritten with \0 when we're done.
     
  // We don't need to populate the *entire* buffer -- we may stop short!
  // The returned value will be the new size of the string.
 
  return actual_new_size;
});

QVarLengthArray不会做多余的初始化,请注意(不过QT这套东西会玩的越来越少了,大部分读者应该不玩QT)

基础知识,不会的可以去看《程序员的自我修养 链接/库》这本书

MSVC有两套coroutine API

// in <experimental/coroutine>
#ifndef _ALLOW_COROUTINE_ABI_MISMATCH
#pragma detect_mismatch("_COROUTINE_ABI", "1")
#endif // _ALLOW_COROUTINE_ABI_MISMATCH

// in <coroutine>
#ifndef _ALLOW_COROUTINE_ABI_MISMATCH
#pragma detect_mismatch("_COROUTINE_ABI", "2")
#endif // _ALLOW_COROUTINE_ABI_MISMATCH

使用 /std:c++20/std:c++latest,才会用最新的api,experimental是c++17旧的

又是Windows API,如何处理flags,我直接贴下面,不懂windows

Flag Meaning Recommendation
DISCONNECTED No network interface detects any network Treat as offline.
NOTRAFFIC An interface is connected, but it cannot send or receive network traffic. Treat as offline.
SUBNET/LOCALNETWORK An interface has been configured to send traffic, but the system cannot confirm Internet connectivity. Make one attempt to contact service.
INTERNET The system has confirmed access to Microsoft Internet sites. Treat as fully online.

没看懂

视频

没啥说的,之前讲过,#embed可以嵌入二进制,比如插个音乐,文本等等

开源项目需要人手

新项目介绍/版本更新

没看懂这玩意是干什么的


本文永久链接

如果有疑问评论最好在上面链接到评论区里评论,这样方便搜索,微信公众号有点封闭/知乎吞评论

看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持! 觉得写的不错那就给点吧, 在线乞讨 微信转账