C语言第三方标准库STC 5.0新版发布

STCLIB

写在前面

C语言第三方标准库STC 5.0新版于今日(2025年1月13日)新鲜发布出炉,首先表示热烈祝贺!

此处所谓“C语言第三方标准库”,是我(LIIGO)个人通俗理解/通俗称谓,STC作者Tyge Løvset(tylov)并没有使用过这种营销语言。在作者的自述里STC是这样的:

STC - Smart Template Containers.
STC is a comprehensive, modern, typesafe and fast templated general purpose container and algorithms library for C99. It aims to make C-programming even more fun, more productive and safer.

翻译:

STC:Smart Template Containers。
STC是一个全面的、现代的、类型安全的、高性能的、模板化的、通用的、基于C99的容器和算法库。它的目标是让C编程更有趣、更便捷、更安全。

STC库的主要功能有:


我与STC相识颇有渊源。早在2021年我就开始试用STC,那时候它的代码是这样的:

using_cvec(i, int);
cvec_i vec = cvec_i_init();

当时就感觉,其通用数据结构cvec, clist, cset, cmap, … 泛型API友好性和一致性俱佳。后来其API风格剧变,但泛型API友好性和一致性依然俱佳。

2021年5月6日,在阅读学习了STC中“宏函数重载,可变参数的宏函数,宏转发”相关源码一周之后,我又完全凭记忆手写了一遍(代码附后),用于加深印象。

2021年5月8日,我给它提过一个功能请求,即 Issue #4,看编号就知道这是最早的Issues之一。作者响应迅速,很快就提供了相关API。

自那以后,我就长期持续关注着STC的开发进展,偶尔提一些小建议,或者偶尔贡献一点点代码。

2023年6月25日,我发表博客预告《即将发布的C库STC v4.3已支持块定义风格的协程》,文中提及STC作者tylov曾经对我提交的PR #57三番五次表示感谢。

总之,我特别看好STC的发展前景。STC是一个很实用、对程序员十分友好的库,它提供了原本应该由C语言官方标准库提供的最基础的API。STC充分运用宏技术实现了类似于泛型的类型安全的API,性能高、易用性上佳。早些年我曾经很反感在C语言代码里大量使用宏(理由是可读性可维护性差),但后来逐渐意识到,离开了宏你很难提供既实用又优雅的API接口。好在STC源码还是有节制的使用宏,没有像M*LIB那样把宏用到极致、用到走火入魔的地步(我自接触STC之后就基本不再关注mlib,zpl,klib,tbox等同类型C库)。

附:我参考早期STC源码手撸的macro_forward宏:

#include <stdio.h>

#define args_count(...) \
    args_16th(0,##__VA_ARGS__,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)

#define args_16th(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,...) \
    _16

#define name_concat2(prefix,n) name_concat2_(prefix,n)
#define name_concat2_(prefix,n) prefix##n

#define macro_forward(prefix, ...) \
     name_concat2(prefix, args_count(__VA_ARGS__))(__VA_ARGS__)

#define test(...) macro_forward(test_,__VA_ARGS__)

#define test_1(x) x
#define test_2(x,y) x+y
#define test_3(x,y,z) x+y+z

int main() {
    int n0 = args_count();
    int n1 = args_count(a);
    int n2 = args_count(a,b);
    int n3 = args_count(a,b,c);
    int n9 = args_count(a,b,c, d,e,f, h,i,j);
    printf("args_count: n0=%d n1=%d n2=%d n3=%d n9=%d\n",
        n0,n1,n2,n3,n9);
    printf("test: %d, %d, %d\n", test(1), test(1,2), test(1,2,3));
    return 0;
}

以下全文引述STC v5.0的新版发布通告。

Release STC v5.0

By Tyge Løvset (tylov), Jan 13, 2025.

Finally after nearly two years, a new release of STC! It has lots of improvements, new features and bug fixes. There are a number of breaking changes since the last released version, so make sure to check out the version history. Special thanks to all who contributed with PRs, bug reports and suggestions!

New Features in v5.0

  • New main build system with Meson. Simple Makefile provided as well.
  • New sum type (tagged union), included via algorithm.h
  • New single/multi-dimensional generic span type, with numpy-like slicing.
  • Coroutines now support structured concurrency and symmetric coroutines.
  • Coroutines now support error handling and error recovery.
  • Template parameter i_type lets you define i_type, i_key, and i_val all in one line (comma separated).
  • Template parameters i_keyclass and i_valclass to specify types with _drop() and _clone() functions defined.
  • Template parameters i_keypro and i_valpro to specify cstr, box and arc types (users may also define pro-types).
  • hmap now uses Robin Hood hashing (very fast on clang compiler).
  • Several new algorithms added, e.g. c_filter (ranges-like).
  • A lot of improvements and bug fixes.

What’s Changed

  • add cco macro, block style coroutine definition by liigo in #57
  • Merge in backported code from master by tylov in #63
  • Dev43 by tylov in #65
  • Use attribute((unused)) for clang too by sgraham in #73
  • Remove trailing whitespace by sgraham in #75
  • singleheader.py fixes for Windows and newer Python by sgraham in #74
  • Fix issue with cbits_resize overwriting data when shrinking by thehoustonian in #94
  • Fix arc memory leak by wmww in #82
  • Detect Clang and Intel compilers for unused attribute. by Melkor-1 in #89
  • fix some mistake in deq_api.md by soyoo in #90
  • Add meson build system support by mochaaP in #98
  • build/meson: fix building as a subproject by mochaaP in #100
  • ci: add meson build/tests with asan/ubsan/msan by mochaaP in #101
  • build: misc changes by mochaaP in #111

New Contributors

Full Changelog: v4.2…v5.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值