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

C++ 中文周刊 第109期

作者头像
王很水
发布2023-04-23 16:59:47
3680
发布2023-04-23 16:59:47
举报

C++ 中文周刊 第109期

弄了个qq频道,手机qq点击进入

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

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

提交 issue

爬山有点累,更新有点晚


资讯

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

三月四月邮件列表 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/#mailing2023-04

有栈协程值得关注 fiber_context。就是boost那套。有概率能进

RCU和Hazard pointer讨论好久了。感觉也是早晚的事。各个组件库都有实现

boost 1.82出了 https://www.boost.org/users/history/version_1_82_0.html

引入boost.mysql库,之前提到过。另外去掉了c++03支持,现在是2023了

文章

尽可能的用explicit,除了以下场景

复制构造A(const A&)/移动 A(A&&)

初始化列表A(std::initializer_list<T>)

tuple类型std::tuple_size_v<A>

类型擦除类型function any

剩下的场景能用就用,尤其是operator bool()

[explicit(To Be

!(To Be))](https://cppsenioreas.wordpress.com/2023/04/10/explicitto-be-to-be/)

这个讲的是这个语法

代码语言:javascript
复制
class integer {
public:
    template<typename T, typename = std::enable_if_v<std::is_arithmetic_v<T>>>
    explicit(std::is_floating_point_v<T>)
    integer(T t) : val(std::round(t)) {}
private:
    int val;
};
void func(integer i) {/*...*/}
{
    // func(3.4); // won't compile
    func(integer(3.4));
    func(5);
}

控制explicit行为

说实话,有点没看懂

代码语言:javascript
复制
#include <algorithm>
#include <array>
#include <cstdint>
#include <string_view>
#include <utility>

// clang-format off
template <std::size_t N>
struct fixed_string final {
    constexpr explicit(false) fixed_string(const char (&str)[N + 1]) {
        std::copy_n(str, N + 1, std::data(data));
    }

    [[nodiscard]] constexpr auto operator<=>(const fixed_string&) const =
        default;

    std::array<char, N + 1> data{};
};

template <std::size_t N> fixed_string(const char (&str)[N]) -> fixed_string<N - 1>;

template <fixed_string>
struct types final {
    friend auto get(types);
    template <class T>
    struct set {
        friend auto get(types) { return T{}; }
    };
};

template <fixed_string Str>  // typename erasure
struct te : decltype(get(types<Str>{})) {};

template<fixed_string Str, class T>
[[nodiscard]] constexpr auto typename_cast(const T& t) {
  void(typename types<Str>::template set<T>{});
  return static_cast<te<Str>>(t);
}

constexpr auto show_types(auto...) -> void;

template <class...> struct foo {};
template <class... Ts>
auto fn() -> void {
    foo<Ts...> foo{};
    show_types(typename_cast<"foo.long">(foo)); // tefoo.long instead of foo<main()::lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type, ...>
}

int main() {
    struct lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type {};

    fn<
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type,
        lllllllllllllllllllllllllllllllllllllllllllllllllllllllllong_type
    >();
}

// clang-format on

https://godbolt.org/z/j3EP4za5q

windows的,没看懂

感觉很厉害

MLIR走读

自己用数组做内存池,然后重载new aligned_malloc,结果地址并没有aligned,原因,数组没对齐

在已有库上拓展c++20协程玩法,代码演进上的一些设计

分析RTTI

四段代码

代码语言:javascript
复制
char table1(int idx) {
  const char array[] = {'z', 'b', 'k', 'd'};
  return array[idx];
}

std::string table2(int idx) {
  const std::string array[] = {"a", "l", "a", "z"};
  return array[idx];
}

std::string table3(int idx) {
  const static std::string array[] = {"a", "l", "a", "z"};
  return array[idx];
}

std::string_view table4(int idx) {
  constexpr static std::string_view array[] = {"a", "l", "a", "z"};
  return array[idx];
}

最后一个最快。constexpr能用就用

使用unique_ptr和vector会遇到个坑爹的问题,从initializer_list 构造的没法move,坑爹initializer_list

就是std::ranges::fold_*

开源项目需要人手

  • asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线
  • Unilang deepin的一个通用编程语言,点子有点意思,也缺人,感兴趣的可以github讨论区或者deepin论坛看一看。这里也挂着长期推荐了
  • paozhu 国人开发的web库,和drogon联系过没共建而考虑自己的需求基于asio开发。感兴趣的可以体验一下,挂在这里长期推荐了

新项目介绍/版本更新

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C++ 中文周刊 第109期
    • 资讯
      • 文章
        • 开源项目需要人手
          • 新项目介绍/版本更新
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档