前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 中文周刊 2024-05-25 第158期

C++ 中文周刊 2024-05-25 第158期

作者头像
王很水
发布2024-07-30 15:20:05
960
发布2024-07-30 15:20:05
举报
文章被收录于专栏:C++ 动态新闻推送

本期文章由 HNY 赞助, 本期内容很少

资讯

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

五月邮件 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/#mailing2024-05

基本就是range constract executor 相关提案乱炖

讲几个R0

  • • executor还没定,基于他的async object就来了。大概意思就是move only function封装用executor来管理/分配内存/生命周期管理,作者还给了POC https://godbolt.org/z/rrbW6veYd 没有细看
  • • Type-aware allocation and deallocation functions https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2719r0.html

简单来说就是针对类级别的更精细的内存控制

代码语言:javascript
复制
operator new(sizeof(T), type_identity<T>{}, args…)

通常这种都是自己搞个数组placement new

提供根据类型的new接口,能更简单实现这种逻辑。看一乐,提供一种思路

  • • P3125R0: Pointer Tagging https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3125r0.pdf

这个其实就是很多指针实际上更多是抽象的,可以利用bit做事,但标准对这块不是很清晰,算是UB吧

比如llvm的PointerIntPair arm的MTE hash trie的指针管理 或者pointer swizzle技巧那种把指针搞出复合语义

这种玩意其实就是tag pointer,不同tag表达不同含义,感觉标准化会更好一些

  • • Floating-Point Maximum/Minimum Function Objects https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3307r0.html

浮点数的min/max处理了太多边角场景(NaN之类的),不够快,不想用ffastmath情况下提供了一套新接口

  • • elide https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3288r0.html

帮你把optional variant any里的T掏出来。这个很干净

实现godbolt https://godbolt.org/z/65fEd3axf

  • • A Relocating Swap https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3239r0.pdf

帮P2786加了个接口实现P1144相同能力。看不懂?没事我也不懂

编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-0522第255期

文章

超好用的 C++ 在线编译器(VSCode 版)https://zhuanlan.zhihu.com/p/694365783

godbolt集成vscode了,挺好的

C++神秘学习——memcpy的一个ub https://zhuanlan.zhihu.com/p/699259091

memcpy data()了,编译器假设不空优化导致asan报错

vector string当buffer没问题,都用他们当buffer不用他们的成员函数复制是怎么个意思,瞧不起assign?

这个也和群友激烈讨论了

笔者的个人观点,data()虽然不是const,但是最好最好不乱玩,能用成员用成员,实在不行才hack,比如resize这种

assign肯定也是memcpy,他会帮你判断的,你直接拿来用,那肯定说明不为空,那编译器的假设笔者认为没毛病

write()和mmap写混用与cache alias https://zhuanlan.zhihu.com/p/699052979

学到了

SLUB 分配器的下一步计划 https://zhuanlan.zhihu.com/p/699058331

看一乐

C++无锁(lock free)数据结构与有锁数据结构相比,速度,性能等有何区别?多线程中如何选择?https://www.zhihu.com/question/558986994/answer/3498768792

看一乐

long double to_string会丢失精度? https://www.reddit.com/r/cpp/comments/1czb8ul/stdto_stringlong_double_is_broken_and_rounds_the/
代码语言:javascript
复制
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    long double d = -20704.000055577169;
    string s = to_string(d);
    cout << s << endl;//-20704.000056 
    cout << setprecision(12) << fixed << s << endl;
}

两个输出没有差别。注意这个坑 godbolt https://godbolt.org/z/9EWYexK6E

Looking up a C++ Hash Table with a pre-known hash https://ebadblog.com/looking-up-a-c++-hash-table-with-a-pre-known-hash

如果你知道了hash,如何避免容器计算hash?定制Equal,把hash比较也放进去,直接看完整代码

代码语言:javascript
复制
template<typename Hash>
class KeyHashPair {
private:
    std::string_view key_;
    std::size_t hash_;
public:
    KeyHashPair() = delete;
    KeyHashPair(std::string_view sv) : key_(sv), hash_(Hash{}(key_)) {}

    std::string_view key() const { return key_; }
    std::size_t hash() const { return hash_; }
};

struct Hash {
    using is_transparent = void;

    std::size_t operator()(std::string_view sv) const {
        return std::hash<std::string_view>{}(sv);
    }
    std::size_t operator()(KeyHashPair<Hash> pair) const {
        return pair.hash();
    }
};

struct KeyEqual {
    using is_transparent = void;

    bool operator()(std::string_view lhs, std::string_view rhs) const {
        return lhs == rhs;
    }
    template<typename Hash>
    bool operator()(KeyHashPair<Hash> lhs, std::string_view rhs) const {
        return lhs.key() == rhs;
    }
};

using Set = std::unordered_set<std::string, Hash, KeyEqual>;

int main() {
    Set set{"foo"};

    const std::string string{"foo"};
    const KeyHashPair<Set::hasher> pair{string};

    assert(set.contains(pair));
}

还是有点思路提升的,但并没有把hash存下来,其实更合理的思路是直接比较set内部的hash,可惜没有暴露

开源项目介绍

  • • https://github.com/lhmouse/asteria 一个脚本语言
  • • https://github.com/odygrd/quill/releases/tag/v4.0.0 4.0发布 编译时间 写入速度双提升
  • • https://github.com/kboutora/ipvar 多进程共享变量,没明白什么场景用得上
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 资讯
  • 文章
    • 超好用的 C++ 在线编译器(VSCode 版)https://zhuanlan.zhihu.com/p/694365783
      • C++神秘学习——memcpy的一个ub https://zhuanlan.zhihu.com/p/699259091
        • write()和mmap写混用与cache alias https://zhuanlan.zhihu.com/p/699052979
          • SLUB 分配器的下一步计划 https://zhuanlan.zhihu.com/p/699058331
            • C++无锁(lock free)数据结构与有锁数据结构相比,速度,性能等有何区别?多线程中如何选择?https://www.zhihu.com/question/558986994/answer/3498768792
              • long double to_string会丢失精度? https://www.reddit.com/r/cpp/comments/1czb8ul/stdto_stringlong_double_is_broken_and_rounds_the/
                • Looking up a C++ Hash Table with a pre-known hash https://ebadblog.com/looking-up-a-c++-hash-table-with-a-pre-known-hash
                • 开源项目介绍
                相关产品与服务
                容器服务
                腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档