frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Detecting if an expression is constant in C

https://nrk.neocities.org/articles/c-constexpr-macro#detecting-if-an-expression-is-constant-in-c
49•signa11•11mo ago

Comments

wahern•11mo ago
> This works. But both gcc and clang warn about the enum being anonymous... even though that's exactly what I wanted to do. And this cannot be silenced with #pragma since it's a macro, so the warning occurs at the location where the macro is invoked.

You can use _Pragma instead of #pragma. E.g.

  #define C(x) ( \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Wvisibility\"") \
    (x) + 0*sizeof(void (*)(enum { tmp = (int)(x) })) \
    _Pragma("clang diagnostic pop") \
  )
EDIT: Alas, GCC is a little pickier about where _Pragma is allowed so you may need to use a statement expression. Also, it seems GCC 14 doesn't have a -W switch that will disable the anonymous enum warning.
pjc50•11mo ago
It's remarkable that people will say that doing this kind of thing is better than learning a language which actually lets you enforce this with the type system.

(or even just insist that users use the version of the language which supports "constexpr"!)

oguz-ismail•11mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•11mo ago
Indeed, usually if I'm using C these days it's because I only have access to a c compiler for my target platform, or because I'm modifying an existing C codebase.
uecker•11mo ago
I do not think anybody said this. The point is that these macros work for early versions of C. If you need to support early versions of C, learning another language is not a solution. If you don't have to, you can use C23's constexpr.
trealira•11mo ago
C used to seem like a beautiful and simple language to me, but as I used it and learned more about it, it seemed more complex under the surface, and kind of janky as well. It's just utilitarian.
wat10000•11mo ago
Learning such a language doesn’t mean I can use it.
o11c•11mo ago
The problem is that no such language exists.

There are many languages that provide one particular feature that C doesn't provide, but they do this at the cost of excluding numerous other features that C widely relies on.

kjs3•11mo ago
"I have no idea what problem you're trying to solve, what the constraints are, what the use cases might be, what tools are available on the platform, what the job or regulations require, what the skillsets of the people involved are, what the timeline is...but I'm absolutely, unshakably certain that I have a magic bullet that will make all your problems go away."

FTFY.

sleirsgoevy•11mo ago
The Linux kernel has even a way to determine whether the expression is compile-time, WITHOUT aborting compilation in either case.

The trick is this (copied vebratim from Linux):

#define __is_constexpr(x) (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

Explanation: if x is a constant expression, then multiplying it by zero yields a constant 0, and casting a constant 0 to void* makes a null pointer constant. And the ternary expression, if one of its sides is a null pointer constant, collapses to the type of the other side (thus the type of the returned pointer will be int*, and the sizeof will match). And if x was not constant, then the lefthand side would not be considered a null pointer constant by type inference, the type of the ternary expression will be void*, and the sizeof check will not match.

With a few more clever tricks, it's even possible to implement a compile-time "type ternary expression", like this: TYPE_IF(2 * 2 == 4, int, long). This is left as an exercise for the reader.

amelius•11mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•11mo ago
It still is a thing, though.
cperciva•11mo ago
With a few more clever tricks...

I did this with my PARSENUM macro (https://github.com/Tarsnap/libcperciva/blob/master/util/pars...) to parse strings into floating-point, unsigned integer, or signed integer types (and check bounds) using a single interface.

bobbyi•11mo ago
I thought this would work:

#define C(x) (sizeof(char[x]), x)

sizeof is a compile-time operation so x need to be known at compile time.

It didn't work as expected. It turns out there is an exception and the standard says that sizeof is actually calculated at runtime specifically for variable length arrays:

> The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Rivian allows you to disable all internet connectivity

https://rivian.com/support/article/can-i-disable-all-data-collection-from-my-vehicle
434•Cider9986•4h ago•170 comments

LinkedIn is scanning browser extensions

https://404privacy.com/blog/linkedin-is-scanning-your-browser-extensions-this-is-how-they-use-the...
407•un-nf•5h ago•180 comments

How Mark Klein told the EFF about Room 641A [book excerpt]

https://thereader.mitpress.mit.edu/the-whistleblower-who-uncovered-the-nsas-big-brother-machine/
406•the-mitr•8h ago•119 comments

Opus 4.7 knows the real Kelsey

https://www.theargumentmag.com/p/i-can-never-talk-to-an-ai-anonymously
125•ilamont•1d ago•62 comments

CopyFail was not disclosed to distro developers?

https://www.openwall.com/lists/oss-security/2026/04/30/10
351•ori_b•8h ago•284 comments

Shai-Hulud Themed Malware Found in the PyTorch Lightning AI Training Library

https://semgrep.dev/blog/2026/malicious-dependency-in-pytorch-lightning-used-for-ai-training/
317•j12y•8h ago•104 comments

I built a Game Boy emulator in F#

https://nickkossolapov.github.io/fame-boy/building-a-game-boy-emulator-in-fsharp/
200•elvis70•7h ago•47 comments

Claude Code refuses requests or charges extra if your commits mention "OpenClaw"

https://twitter.com/theo/status/2049645973350363168
929•elmean•10h ago•524 comments

Snowball Earth may hide a far stranger climate cycle than anyone expected

https://sciencex.com/news/2026-04-snowball-earth-stranger-climate.html
32•wglb•2h ago•4 comments

How an oil refinery works

https://www.construction-physics.com/p/how-an-oil-refinery-works
308•chmaynard•10h ago•92 comments

The upsell game – Vercel upselling tactics revealed

https://theupsellgame.com/
79•bartoindahouse•4h ago•12 comments

Reverse Engineering SimTower

https://phulin.me/blog/simtower
98•patrickhulin•2d ago•15 comments

Belgium stops decommissioning nuclear power plants

https://dpa-international.com/general-news/urn:newsml:dpa.com:20090101:260430-930-14717/
738•mpweiher•12h ago•684 comments

New mechanical panoramic film camera from Jeff Bridges

https://wideluxx.com
63•armadsen•2d ago•27 comments

You can beat the binary search

https://lemire.me/blog/2026/04/27/you-can-beat-the-binary-search/
245•vok•3d ago•117 comments

Durable queues, streams, pub/sub, and a cron scheduler – inside your SQLite file

https://honker.dev/
169•ferriswil•10h ago•49 comments

Full-Text Search with DuckDB

https://peterdohertys.website/blog-posts/full-text-search-w-duckdb.html
83•ethagnawl•6h ago•19 comments

The Accidental Ancestor – How Verifying Numbers Shaped Modern Hashing

https://0xkrt26.github.io/math_behind_security/2026/04/28/the-accidental-ancestor-Luhn-algorithm....
6•denismenace•2d ago•0 comments

The Church Rock Uranium Mill Spill

https://en.wikipedia.org/wiki/Church_Rock_uranium_mill_spill
53•Sir_Twist•2d ago•4 comments

AI discovery reveals DNA isn’t locked away in cells after all

https://gladstone.org/news/ai-discovery-reveals-dna-isnt-locked-away-cells-after-all
13•hhs•2h ago•2 comments

$500M for Virtual Biology Initiative, Funded by Zuckerbergs

https://biohub.org/news/virtual-biology-initiative/
8•warbaker•32m ago•0 comments

SimpleX Channels, SimpleX Network Consortium and Community Crowdfunding

https://simplex.chat/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speec...
16•pmw•4h ago•1 comments

Does Postgres Scale?

https://www.dbos.dev/blog/benchmarking-workflow-execution-scalability-on-postgres
75•KraftyOne•6h ago•30 comments

I aggregated 28 US Government auction sites into one search

https://bidprowl.com
234•scarsam•12h ago•71 comments

A Milestone in Formalization: The Sphere Packing Problem in Dimension 8

https://www.alphaxiv.org/abs/2604.23468
10•measurablefunc•2d ago•0 comments

Compositing and Blending – Exploring the math and intuition behind blend modes

https://nik.digital/posts/compositing-blending
11•OuterVale•1d ago•1 comments

Spain's parliament will act against massive IP blockages by LaLiga

https://www.democrata.es/en/politics/congress-and-senate/congress-will-act-against-massive-ip-blo...
398•akyuu•9h ago•169 comments

Recovering files from beyond the grave using PhotoRec

https://lost-number.bearblog.dev/recovering-files-from-beyond-the-grave-using-photorec/
49•speckx•7h ago•11 comments

Show HN: Pu.sh – a full coding-agent harness in 400 lines of shell

https://pu.dev/
63•nahimn•3h ago•19 comments

10Gb/s Ethernet: what I did to get it working in my home

https://www.gilesthomas.com/2026/04/10g-ethernet-what-i-did
136•gpjt•1d ago•101 comments