frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

C++26 Contracts: Introduction to Preconditions and Postconditions

https://www.cppstories.com/2026/cpp26_contracts/
24•ibobev•1h ago

Comments

bluGill•34m ago
I'm looking forward to seeing where these go in the future. Can solvers prove my code correct (at least in some trivial cases)? What should happen in a unit test (sometimes I want to test that my program handles out of range values, sometimes it is expensive top check and so I put the contract in to avoid the check at runtime...) Can/should the optimizer account for contracts (same question as unit tests).
klibertp•16m ago
See the papers "Soft Contract Verification" and "Soft Contract Verification for Higher-Order Stateful Programs" (Nguyễn et al.) In general, there's a lot of academic work on the topic coming from the Racket community.

In short: yes, some classes of contracts can be statically proven. Not all of them, and it depends on the language's semantics, so it's probably not easy to retrofit into C++. But there's work being done on unifying types and contracts, moving back and forth between static proof and dynamic enforcement, and it's all pretty interesting. Proving contracts statically eliminates runtime overhead; generating contracts from types makes it possible for a gradually typed language to be sound.

literallyshared•33m ago
I can see the appeal of having 'code as documentation' in header files, but personally I find any type of runtime asserts to be too heavy handed. Graceful error handling, in my opinion, is always preferable. This type of functionality is what is continuing to drive me away from C++. If your preconditions and postconditions matter, document them in a comment block.
ahartmetz•27m ago
You can set a "contract evaluation semantic" for contracts. One of them is "observe", which only prints a message on contract violations. With "ignore", nothing at all happens.
bluGill•12m ago
Graceful handling is a trade off. Sometimes the check is expensive, if everybody normally does it right you don't want to check (particularly in a tight loop), but you don't want to lose this. I work in embedded, in production our CPUs are not powerful enough to risk all those runtime checks, but I want to turn them on when I work on my workstation - in hopes that if there is a problem we catch it.

Contacts are also a guaranteed syntax. Runtime checks provide no way for tools to know if the condition is expected to never happen - this is a bug; vs this is a normal user error we need to check for. A contract means theorem provers can find code paths where a bug could happen - of course we don't have theorem provers for C++, but this is seen as a requirement to get them.

randyrand•30m ago
Maybe just me, but huge complicated languages like C++ seem as unappealing as ever.

If I’m going to write code by hand, the requirement for a beautiful pleasant language is bigger than ever.

ahartmetz•23m ago
On the one hand, almost everyone (including C++ fans) agrees that the language is too big. On the other hand, comtracts just formalize an existing and useful concept.
preommr•28m ago
I am very bullish on constraints based programming like this.

The main problem is code organization and syntax. Tacking stuff like this onto fuction declarations is too basic.

Ideally, I think we need better constructs, e.g. dettached constraints like to the function declaration. You can define constraints separately, in the same namespace or w/e, add notes, comments, etc. and the function declaration can be used with intellisense to jump to the constraint. You could even tag the defintions.

In modern AI-based workflows, the agent can pull up an explanation on the function, and point directly to the constraints, with strong confidence esp. if it's got explicit metadata.

klibertp•5m ago
Take a look at Racket (or Rhombus if allergic to parens). Support for contracts there is stellar and provides everything you mentioned. You can use `define/contract` for inline contracts, or you can attach contracts when exporting names from a module using `contract-out` declaration. Contracts are first-class, can be defined separately and attached wherever you want (importantly, this includes values (structs, classes) as well as functions). There are higher-order contracts that generate new contracts from existing ones. There's support for both static and dynamic metadata for almost all kinds of entities in code (ie. both the syntax and the semantic entity it produces can be annotated with anything). I wouldn't recommend using Racket/Rhombus in production, but it's a great preview of what might become commonplace in a decade or two :)
ZuLuuuuuu•27m ago
This reminds me the "Design by Contract" concept from Eiffel programming language:

https://en.wikipedia.org/wiki/Design_by_contract

https://www.eiffel.com/values/design-by-contract/introductio...

gavinray•21m ago
If you enjoy this style of programming, I wrote a bit on why contracts and capabilities (ie "allocates memory", "performs i/o") are going to become essential for LLM generated code:

https://gavinray97.github.io/blog/design-by-contract-and-eff...

hsaliak•18m ago
id' normally hate this but with agents this is awesome
beached_whale•5m ago
An anthropic employee did a talk earlier this year or last and i think it was said that yeah, they eat it up. Tools to find errors because it is visible is great
nananana9•12m ago
Where do I put these?

If I put them in the header, I have to include half the world because I can't forward declare `Foo foo` argument if I want to check `foo.size() > 3` or similar in a pre/post-condition. This is a non-starter, keeping headers lean is the only way to not end up with 1 hour compiles.

If I put them in the cpp, they lose their only advantage over asserts - that they also double as documentation in the headers.

Did we add 50 pages to the standard for a slightly fancier assert? I feel like this could've been one builtin and a footnote:

contract_assert(expr) - if expr evaluates to false, the behavior of the program is undefined. Implementations are encouraged to emit a runtime check for this in debug builds, or a compiler error in cases where they can statically determine, that the assert will fail.

Libreboot 26.10 "Ominous Olive" released with UEFI, Chromebook support

https://libreboot.org/news/libreboot2610.html
99•iamnothere•1h ago•41 comments

Israeli firm's digital ads enabling installation of 'zero-click' spyware

https://www.theguardian.com/technology/2026/oct/15/israeli-firm-digital-ads-enabling-installation...
216•chunkyslink•1h ago•68 comments

C++26 Contracts: Introduction to Preconditions and Postconditions

https://www.cppstories.com/2026/cpp26_contracts/
25•ibobev•1h ago•14 comments

Things to know when you get cancer

https://jjmaxwell4.com/writing/things-to-know-when-you-get-cancer/
1685•jjmaxwell4•19h ago•403 comments

Wanted to download an ISO, ended up writing a kernel exploit

https://blog.dfb.sh/blog/eap770-root-for-two-zeros/
232•0xdeafbeef•2d ago•121 comments

Show HN: Unfenced – Land for sale that borders public land

https://unfenced.land/
100•otherwatersflow•2h ago•59 comments

The Haunted Toys R Us

https://www.retroist.com/p/the-haunted-toys-r-us
27•speckx•2d ago•8 comments

I know why Mathematicians weep

https://www.derekthompson.org/p/i-know-why-the-mathematicians-weep
54•RickJWagner•47m ago•53 comments

OpenCode 2.0

https://anoma.ly/notes/opencode-2-0/
752•gmays•17h ago•319 comments

Wayland desktop for Linux with skeuomorphic design

https://github.com/fyzugx/DEsk
9•FinalDestiny•1h ago•0 comments

Tiny Little Office

https://tinylittleoffice.com/
559•donohoe•16h ago•132 comments

The First Social Network: In rural Argentina residents send messages w AM radio

https://www.newyorker.com/magazine/2026/10/19/the-first-social-network
81•bookofjoe•1d ago•33 comments

GridSnap – an open-source Divvy-style window snapper for macOS

https://github.com/konieczkow/gridsnap
7•zderror•1h ago•3 comments

Zero-Click Spyware Delivered via Targeted Ads: AdFection

https://citizenlab.ca/research/how-discover-tech-turned-targeted-ads-into-a-pipeline-for-spyware/
26•jcomp•3h ago•3 comments

Show HN: Indigene – Native plants for where you stand

https://indigene.app/
76•olivierlacan•2d ago•12 comments

Do dentists know what they're doing?

https://mapwriting.substack.com/p/dentists
843•doitLP•3d ago•612 comments

Catch a Bird – I made it for my own sanity

https://catchabird.com
296•eduardsik•3d ago•85 comments

Americans' social activity has steeply declined in the past 30 years

https://www.dataforprogress.org/blog/2026/9/29/americans-social-activity-has-steeply-declined-in-...
495•hubraumhugo•22h ago•454 comments

LFM2.5-24B-A2B: Built for fast on-device agents

https://www.liquid.ai/blog/lfm2-5-24b-a2b
257•gmays•14h ago•64 comments

Tracing Mystery Network Traffic with eBPF Across HRT's Global Compute Cluster

https://www.hudsonrivertrading.com/hrtbeat/long-on-research-short-on-bandwidth/
25•theHRTBeat•2d ago•3 comments

It took me nine years to hack my Xbox 360

https://kennedn.com/blog/posts/glitching/
138•kennedn•2d ago•35 comments

Sticky alpine flower confirms Darwin's 150-year-old carnivorous plant prediction

https://phys.org/news/2026-08-sticky-alpine-darwin-year-carnivorous.html
71•maxall4•2d ago•29 comments

Show HN: A 3D Configurator for Macropads

https://npad.ngrok.dev
3•alexandermkinn•1d ago•1 comments

Donaldson Futuro House in Idyllwild, California, USA

https://www.wowhaus.co.uk/2026/10/12/donaldson-futuro-house-in-idyllwild-california-usa/
72•rmason•1d ago•24 comments

Stopping sand from destroying a hydropower turbine [video]

https://www.youtube.com/watch?v=kCYrSBexF_4
22•CaliforniaKarl•2d ago•3 comments

Show HN: Ito – Build Terminal UIs in Lua

https://uji.sh/ito/
53•cat-whisperer•16h ago•10 comments

Show HN: Flipflop, Iroh-powered persistent tunnels for filesharing

https://github.com/andunieee/flipflop
72•andunie•10h ago•30 comments

Better HTTP/1 connection reuse in Go 1.27

https://go.dev/blog/http-connection-reuse
282•allddd•22h ago•45 comments

David Rudnick on "borderline impossible" process of designing Arsenal typeface

https://www.itsnicethat.com/features/david-rudnick-johnnie-campbell-arsenal-rocastle-typeface-gra...
50•droidjj•2d ago•31 comments

The Double Headed Stack Allocator

https://blog.demofox.org/2026/10/03/the-double-headed-stack-allocator/
82•g0xA52A2A•12h ago•28 comments