C++ 中文周刊 第109期

周刊项目地址

公众号

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讨论好久了。感觉也是早晚的事。各个组件库都有实现

4月22日深圳线下聚会!技术分享征集中

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

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()

这个讲的是这个语法

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行为

说实话,有点没看懂

#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

四段代码

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_*

开源项目需要人手

新项目介绍/版本更新


本文永久链接

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

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