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

C++ 中文周刊 2024-02-17 第149期

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

本期文章由 不语 黄亮Anthony Tudou kenshin 赞助

勉强算半期吧,返程没时间了,简单更新一下

资讯

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

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

二月邮件 https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/#mailing2024-02

其实重点比较明朗,就execution reflect graph这些,剩下的基本都是修正

fiber_context也好久了

文章

Clang 出了 Bug 怎么办?来一起修编译器吧! https://zhuanlan.zhihu.com/p/659944720

看一遍基本就把clang llvm这套东西串起来了。都学一下吧,llvm战未来朋友们

C++20的constexpr string为什么无法工作 https://www.zhihu.com/question/643989678/answer/3393477151

感受抽象的gcc sso优化实现

另外clang本来是没做constexpr sso优化的,最近又给加上了 https://www.zhihu.com/question/643989678/answer/3393744969

微信群里也讨论了,咨询了maskray老师意见,可能就是为了对齐libstdcxx的行为

我和这个想法相同,你都constexpr了,还sso干啥

C++ 中 constexpr 的发展史!(上) https://zhuanlan.zhihu.com/p/682031684

感觉有点不认识const了

[VLDB'22] Velox: Meta’s Unified Execution Engine https://zhuanlan.zhihu.com/p/682036952

还挺有意思的

too dangerous for c++ https://blog.dureuill.net/articles/too-dangerous-cpp/

对比rust c++的shared_ptr没有太限制生命周期,可能会用错,c++还是太自由了

On the virtues of the trailing comma https://devblogs.microsoft.com/oldnewthing/20240209-00/?p=109379

就是这种行尾的逗号,对于git merge也友好

代码语言:javascript
复制
// C, C++
Thing a[] = {
    { 1, 2 },
    { 3, 4 },
    { 5, 6 },
    //      ^ trailing comma
};

// C#
Thing[] a = new[] {
    new Thing {
        Name = "Bob",
        Id = 31415,
        //        ^ trailing comma
    },
    new Thing {
        Name = "Alice",
        Id = 2718,
        //       ^ trailing comma
    },
//   ^ trailing comma
};

Dictionary d = new Dictionary<string, Thing>() {
    ["Bob"] = new Thing("Bob") { Id = 31415 },
    ["Alice"] = new Thing("Alice", 2718),
    //                                  ^ trailing comma
};

感觉这是个不成文规定实现

Formatting User-Defined Types in C++20 https://www.modernescpp.com/index.php/formatting-user-defined-types-in-c20/

简单实现

代码语言:javascript
复制
#include <format>
#include <iostream>

class SingleValue {
 public: 
    SingleValue() = default;
    explicit SingleValue(int s): singleValue{s} {}
    int getValue() const {
        return singleValue;
    }
 private:
    int singleValue{};
};

template<>
struct std::formatter<SingleValue> : std::formatter<int> {             // (1)
  auto format(const SingleValue& singleValue, std::format_context& context) const {
    return std::formatter<int>::format(singleValue.getValue(), context);
  }
};

int main() {
    SingleValue singleValue0;
    SingleValue singleValue2020{2020};
    SingleValue singleValue2023{2023};

    std::cout << std::format("{:*<10}", singleValue0) << '\n';
    std::cout << std::format("{:*^10}", singleValue2020) << '\n';
    std::cout << std::format("{:*>10}", singleValue2023) << '\n';
}
Visual overview of a custom malloc() implementation https://silent-tower.net/projects/visual-overview-malloc

典型内存池实现介绍

Vectorizing Unicode conversions on real RISC-V hardware https://camel-cdr.github.io/rvv-bench-results/articles/vector-utf.html

哥们看不懂rsicv 不太懂

C++20 Concepts Applied – Safe Bitmasks Using Scoped Enums https://accu.org/journals/overload/32/179/fertig/

直接贴代码吧

代码语言:javascript
复制
template<typename T>
constexpr std::
  enable_if_t<
    std::conjunction_v<std::is_enum<T>,
      // look for enable_bitmask_operator_or
      // to  enable this operator ①
      std::is_same<bool,
        decltype(enable_bitmask_operator_or(
          std::declval<T>()))>>,
  T>
operator|(const T lhs, const T rhs) {
  using underlying = std::underlying_type_t<T>;
  return static_cast<T>(
    static_cast<underlying>(lhs) |
    static_cast<underlying>(rhs));
}
namespace Filesystem {
  enum class Permission : uint8_t {
    Read = 1,
    Write,
    Execute,
  };
  // Opt-in for operator| ②
  constexpr bool 
    enable_bitmask_operator_or(Permission);
} // namespace Filesystem

这个玩法就是针对部分提供enable_bitmask_operator_or 的enum class 提供 operator |

现在是2024了,有没有新的玩法

concept

代码语言:javascript
复制
template<typename T>
requires(std::is_enum_v<T>and requires(T e) {
  // look for enable_bitmask_operator_or to
  // enable this operator ①
  enable_bitmask_operator_or(e);
}) constexpr auto
operator|(const T lhs, const T rhs) {
  using underlying = std::underlying_type_t<T>;
  return static_cast<T>(
    static_cast<underlying>(lhs) |
    static_cast<underlying>(rhs));
}
namespace Filesystem {
  enum class Permission : uint8_t {
    Read    = 0x01,
    Write   = 0x02,
    Execute = 0x04,
  };
  // Opt-in for operator| ②
  consteval 
    void enable_bitmask_operator_or(Permission);
} // namespace Filesystem

c++23 有to_underlying了

代码语言:javascript
复制

template<typename T>
requires(std::is_enum_v<T>and requires(T e) {
  enable_bitmask_operator_or(e);
}) constexpr auto
operator|(const T lhs, const T rhs)
{
  return static_cast<T>(std::to_underlying(lhs) |
                        std::to_underlying(rhs));
}

简洁一丢丢

开源项目介绍

  • • https://github.com/lhmouse/asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群753302367和作者对线
  • • https://github.com/linuxdeepin/unilang deepin的一个通用编程语言,点子有点意思,也缺人,感兴趣的可以github讨论区或者deepin论坛看一看。这里也挂着长期推荐了
  • • https://github.com/statisch/graphiz 一个图遍历演示库,挺好玩的
  • • https://github.com/Janos95/mantis P2M: A Fast Solver for Querying Distance from Point to Mesh Surface https://yuemos.github.io/Projects/P2M/pdf/P2M_author.pdf 的实现

互动环节

我不想返工啊

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 资讯
  • 文章
    • Clang 出了 Bug 怎么办?来一起修编译器吧! https://zhuanlan.zhihu.com/p/659944720
      • C++20的constexpr string为什么无法工作 https://www.zhihu.com/question/643989678/answer/3393477151
        • C++ 中 constexpr 的发展史!(上) https://zhuanlan.zhihu.com/p/682031684
          • [VLDB'22] Velox: Meta’s Unified Execution Engine https://zhuanlan.zhihu.com/p/682036952
            • too dangerous for c++ https://blog.dureuill.net/articles/too-dangerous-cpp/
              • On the virtues of the trailing comma https://devblogs.microsoft.com/oldnewthing/20240209-00/?p=109379
                • Formatting User-Defined Types in C++20 https://www.modernescpp.com/index.php/formatting-user-defined-types-in-c20/
                  • Visual overview of a custom malloc() implementation https://silent-tower.net/projects/visual-overview-malloc
                    • Vectorizing Unicode conversions on real RISC-V hardware https://camel-cdr.github.io/rvv-bench-results/articles/vector-utf.html
                      • C++20 Concepts Applied – Safe Bitmasks Using Scoped Enums https://accu.org/journals/overload/32/179/fertig/
                      • 开源项目介绍
                      • 互动环节
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档