本文建立在 Bjarne Stroustrup 在《Software for Infrastructure》中所提出的技术之上,该技术利用 C++ 模板让编译器感知数值的单位,从而能够验证单位使用的一致性,并防止诸如 NASA 1999 年单位混用错误那样的灾难。作者将这一思想加以扩展,通过教会编译器在编译期对单位进行算术运算,从而支持任意数量的基本单位以及呈线性关系的单位(例如厘米、米和英寸)。
In Software for Infrastructure,
Bjarne Stroustrup shows a way to use templates to make the C++ compiler
aware of the unit of a value (e.g. kilograms, seconds, etc.), such that it can check consistent use
and prevent disasters like the well known
error at NASA in 1999
caused by mixing incompatible units.
In this article, I show how to extend this idea to support any number of base units and
linearly related units (e.g. centimetres, metres and inches) by teaching
the compiler how to do arithmetic on units.
继续阅读全文
为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。
AI 摘要This blog post explores Rust's fmt::Arguments type and the format_args!() macro, which power common text formatting macros such as print!(), format!(), and log::info!(). The author describes fmt::Arguments as a great building block that is indirectly used in nearly every Rust program. The post covers how it works, how it is implemented today, and how it might change in the future.
AI 摘要This article explores the complicated and often overlooked topic of temporary lifetimes in Rust, noting that while simple cases work automatically, there are many situations where temporaries don't last exactly as long as desired. The authors rediscover the rules governing temporary lifetimes, examine several use cases for temporary lifetime extension, and introduce a proposed new language feature called 'super let' that would give developers more control over temporary lifetimes.
AI 摘要This blog post examines whether Rust should receive a formal standards document like those that exist for C and C++. The author argues that while Rust needs an accurate specification, full 'standardization' may not be necessary depending on how it is defined. The post also provides an overview of the current state of Rust's stability guarantees and specification efforts.
AI 摘要The article compares Rust's concurrency library with C++'s, noting that Rust's standard library features—threads, atomics, mutexes, and condition variables—closely resemble those introduced in C++11. C++ has since gained additional concurrency features through C++17 and C++20, with more proposals for future versions. The article reviews these C++ features, discusses what Rust equivalents could look like, and what it would take to implement them.
AI 摘要The author recounts how, through an unexpected chain of events, they implemented a conversion from 128-bit integers to 64-bit floats that turned out to be faster than the built-in conversion of every compiler they tested. The blog post explains what happened, how floats work, how the conversion functions, and how the project got a bit out of hand.