Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++ 中文周刊 第143期

C++ 中文周刊 第143期

作者头像
王很水
发布于 2024-07-30 06:47:37
发布于 2024-07-30 06:47:37
9200
代码可运行
举报
运行总次数:0
代码可运行

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

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

请后台留言

另外公众号挂了c++templates 第二版优惠

从上面的链接里下单的兄弟买书到货后可以找我退佣金,加我微信,公众号后台回复即可

本期文章由 黄亮anthony 不语 赞助

资讯

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

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

委员会邮件 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/#mailing2023-12

本月委员会邮件没有什么新鲜的,顶多fiber_context。这里不展开了

文章

Did you know that C++26 added Pack Indexing? https://github.com/tip-of-the-week/cpp
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
template<auto N> consteval auto nth(auto... ts) { return ts...[N]; }
static_assert(1 == nth<0>(1, 2, 3));
static_assert(2 == nth<1>(1, 2, 3));
static_assert(3 == nth<2>(1, 2, 3));

还不确定有什么作用

The double life of objects https://akrzemi1.wordpress.com/2023/12/18/the-double-life-of-objets/

const的副作用

经典例子

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
int main() {
  const int i = 9;
  int& j = const_cast<int&>(i);
  j = 4;
  std::cout << i << std::endl; // prints 9
  std::cout << j << std::endl; // prints 4
}

https://godbolt.org/z/vGG3cdavE

但是这个例子,返回优化了,即使没有实现move

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <cassert>

class Rng {
    int _min;
    int _max;
    // invariant: _min <= _max

public:
    Rng(int lo, int hi) 
    // precond: lo <= hi
    : _min(lo), _max(hi)
    { assert(_min <= _max); }

    int const& min() const { return _min; }
    int const& max() const { return _max; }

    void set(int lo, int hi)
    // precond: lo <= hi
    {
        _min = lo;
        _max = hi;
        assert(_min <= _max); 
    }

    Rng(Rng&&) { assert(false); } // this is never called
    Rng(Rng const&) { assert(false); } // this is never called
};

const Rng foo() {
  const Rng r {1, 2};
  std::cout << &r << std::endl;
  return r;
}

Rng bar() {
    Rng r = foo();
    r.set(3, 4);
    std::cout << &r << std::endl;
    return r;
}

int main() {
    const Rng z = bar();
    std::cout << &z << std::endl;
}

https://godbolt.org/z/n9nn5GjMM

注意这两个例子的区别,统一作用域上的修改

上面的这个xyz 本质上就是一个对象,和第一个例子同一个域里const_cast导致变化不同

About time - how to unit test code that depends on time https://playfulprogramming.blogspot.com/2023/12/about-time.html

怎么mock时间?比如特化?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
template <typename ...>
constexpr auto clock_impl = std::chrono::some_clock{};

template <typename ... Ts>
struct app_clock {
    static
    std::chrono::some_clock::time_point now()
    {
        return clock_impl<Ts...>.now();
    }
};
struct test_clock {
    using time_point = std::chrono::some_clock::time_point;
    static time_point now() { return {};}
};

template <>
constexpr auto clock_impl<> = test_clock{};

https://godbolt.org/z/GbWYaGc7q

浮点数误差入门 https://zhuanlan.zhihu.com/p/673320830?utm_psn=1721654566368694272

讲的不错

linux kernel list 为什么用WRITE_ONCE?https://www.zhihu.com/question/404513670/answer/3323448915?utm_psn=1721654125845192704

写的很有深度,值得一看

从一个crash问题展开,探索gcc编译优化细节 https://zhuanlan.zhihu.com/p/673049367?utm_psn=1721643480974217216

省流 arm O3 优化bug

Trivial Auto Var Init Experiments https://serge-sans-paille.github.io/pythran-stories/trivial-auto-var-init-experiments.html

-ftrivial-auto-var-init=[pattern|zero|uninitialized]

帮助自动初始化栈上的局部变量

开销很大,研究了一圈放弃了

Two kinds of function template parameters https://quuxplusone.github.io/blog/2023/12/17/function-template-parameters/

一种是make_unique这种需要指定T的,一种是swap sort这种不指定T的

如何跨过这种边界,有设计,比如CTAD,但这并不建议使用

那就只能多提供重载了,比如optional

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
template<class T, class A>
optional<T> make_optional(A);
template<class A>
optional<A> make_optional(A);

然后她举了个例子,怎么设计强制制定T和忽略T

https://godbolt.org/z/h38PhG3Y6

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <type_traits>
#include <iostream>

//template<class T, class A>
//T implicitly_convert_to(std::type_identity_t<A>) = delete;

template<class T, class A,
         std::enable_if_t<std::is_convertible_v<A, T>, int> E = 0>
T implicitly_convert_to(A arg) { return T(arg); }

int main() {
  //auto i0 = implicitly_convert_to(9.9999999);
  //std::cout << i0 << "\n";
  auto i1 = implicitly_convert_to<int>(9.9999999);
  std::cout << i1 << "\n";

  //auto j2 = implicitly_convert_to<int, float>(9.9999999);
  //std::cout << j2 <<"\n";
  return 0;
}

看一乐

A Coroutines-Based Single Consumer – Single Producer Workflow by Ljubic Damir https://www.modernescpp.com/index.php/a-coroutines-based-single-consumer-single-producer-workflow-by-ljubic-damir/

直接贴代码了

https://godbolt.org/z/MvYfbEP8r

https://godbolt.org/z/57zsK9rEn

设计的挺有意思的,鉴于篇幅,放在后面

手动优化C++代码来加快编译速度?! https://zhuanlan.zhihu.com/p/673852429

constexpr的代码 编译器没有做充分的优化。这可能加剧编译时长

算是个坑爹细节。运行时能充分优化的代码到了constexpr期反而没优化了

Raymond windows环节,看不懂
  • • How do I specify an optional string parameter to a Windows Runtime method? https://devblogs.microsoft.com/oldnewthing/20231215-00/?p=109155
  • • If the RegisterClass function takes ownership of the custom background brush, why is it leaking? https://devblogs.microsoft.com/oldnewthing/20231218-00/?p=109163
  • • How do I get access to the wParam and lParam of the WM_QUERY­END­SESSION method from my MFC message handler? https://devblogs.microsoft.com/oldnewthing/20231221-00/?p=109174

视频

Cache-friendly Design in Robot Path Planning with C++ - Brian Cairl - CppCon 2023 https://www.youtube.com/watch?v=Uw7FF5MLxZE&ab_channel=CppCon

寻路算法,A*之类的,如何缓存友好。STL不太行

valgrind 也可以测试cache性能,判断miss

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
valgrind --tool=cachegrind --cache-sim=yes

perf也可以,就不说了

结论就是 顺序访问 不要跳转 只访问用到的数据 s执行路径里没有malloc

比如std::unordered_multimap::equal_range 内存不连续,miss就很多

"Distributed Ranges": Model for Building Distributed Data Structures, Algorithms & Views - Ben Brock https://www.youtube.com/watch?v=X_dlJcV21YI&ab_channel=CppCon

概念很帅,把range推广到分布式,做的一些工作

代码在这里 https://github.com/oneapi-src/distributed-ranges/tree/main

开源项目推荐

  • • asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群753302367和作者对线,最近更新了很多文档
  • • Unilang deepin的一个通用编程语言,点子有点意思,也缺人,感兴趣的可以github讨论区或者deepin论坛看一看。这里也挂着长期推荐了
  • • https://github.com/infiniflow/infinity 一个向量数据库,用了c++20的一些特性,挺有意思
  • • https://github.com/avaneev/komihash 一个hash库,速度比xxhash wyhash快,挺有意思的
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-12-25,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++ 中文周刊 第140期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2024/07/30
1190
C++ 中文周刊 第140期
C++ 中文周刊 第142期
周刊项目地址 https://github.com/wanghenshui/cppweeklynews
王很水
2024/07/30
1390
C++ 中文周刊 第142期
C++ 中文周刊 第97期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2023/02/02
5390
C++ 中文周刊 第97期
C++ 中文周刊 第103期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2023/03/11
2910
C++ 中文周刊 第125期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-08-02 第213期
王很水
2024/07/30
680
C++ 中文周刊 第125期
C++ 中文周刊 第86期
从reddit/hackernews/lobsters/meetingcpp/purecpp/知乎/等等摘抄一些c++动
王很水
2022/11/12
2230
C++ 中文周刊 2024-04-06 第153期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2024/07/30
1020
C++ 中文周刊 2024-04-06 第153期
C++ 中文周刊 第128期
周刊项目地址 https://github.com/wanghenshui/cppweeklynews
王很水
2024/07/30
1030
C++ 中文周刊 第128期
C++ 中文周刊 2024-11-16 第172期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-11-13 第280期
王很水
2024/11/18
1080
C++ 中文周刊 2024-11-16 第172期
C++ 中文周刊 第129期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-08-30 第217期
王很水
2024/07/30
1360
C++ 中文周刊 第129期
C++ 中文周刊 第122期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-07-12 第210期
王很水
2024/07/30
1050
C++ 中文周刊 第122期
C++ 中文周刊 第134期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2024/07/30
1330
C++ 中文周刊 第134期
C++ 中文周刊 第89期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2022-11-16 第176期
王很水
2022/11/24
4730
make_shared 如何绕过私有构造函数? C++ 中文周刊 2024-11-23 第173期
https://johnfarrier.com/exploring-c-stdspan-part-4-const-correctness-and-type-safety/?utm_source=rss&utm_medium=rss&utm_campaign=exploring-c-stdspan-part-4-const-correctness-and-type-safety
王很水
2024/12/02
1410
make_shared 如何绕过私有构造函数? C++ 中文周刊 2024-11-23 第173期
C++ 中文周刊 第133期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2024/07/30
1150
C++ 中文周刊 第133期
C++ 中文周刊 第131期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-09-06 第218期
王很水
2024/07/30
1300
C++ 中文周刊 第131期
C++ 中文周刊 2024-05-04 第156期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-05-01 第252期
王很水
2024/07/30
1210
C++ 中文周刊 2024-05-04 第156期
C++ 中文周刊 第136期
RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
王很水
2024/07/30
1110
C++ 中文周刊 第136期
C++ 中文周刊 2024-06-02 第159期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-05-29 第256期
王很水
2024/07/30
1390
C++ 中文周刊 2024-06-02 第159期
C++ 动态新闻推送 第60期
编译器信息最新动态推荐关注hellogcc公众号 2022-04-27 第147期
王很水
2022/04/30
4330
相关推荐
C++ 中文周刊 第140期
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验