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.

Vercel April 2026 security incident

https://www.bleepingcomputer.com/news/security/vercel-confirms-breach-as-hackers-claim-to-be-sell...
672•colesantiago•16h ago•382 comments

A Brief History of Fish Sauce

https://www.legalnomads.com/fish-sauce/
157•vinhnx•22h ago•63 comments

Stop trying to engineer your way out of listening to people

https://ashley.rolfmore.com/stop-trying-to-engineer-your-way-out-of-listening-to-people/
135•walterbell•10h ago•45 comments

Claude Token Counter, now with model comparisons

https://simonwillison.net/2026/Apr/20/claude-token-counts/
56•twapi•5h ago•19 comments

The Bromine Chokepoint

https://warontherocks.com/cogs-of-war/the-bromine-chokepoint-how-strife-in-the-middle-east-could-...
187•crescit_eundo•12h ago•94 comments

Monumental ship burial beneath ancient Norwegian mound predates the Viking Age

https://phys.org/news/2026-04-monumental-ship-burial-beneath-ancient.html
25•pseudolus•2d ago•5 comments

Zero-copy protobuf and ConnectRPC for Rust

https://medium.com/@iainmcgin/zero-copy-protobuf-and-connectrpc-for-rust-69bda8ac0f02
17•PaulHoule•3d ago•5 comments

Show HN: Run TRELLIS.2 Image-to-3D generation natively on Apple Silicon

https://github.com/shivampkumar/trellis-mac
138•shivampkumar•6h ago•21 comments

Mechanical Keyboard Sounds – A listening Museum

https://sheets.works/data-viz/keyboard-sounds
85•akashwadhwani35•4d ago•30 comments

Turtle WoW classic server announces shutdown after Blizzard wins injunction

https://www.pcgamer.com/games/world-of-warcraft/turtle-wow-classic-server-announces-shutdown-afte...
191•Brajeshwar•14h ago•165 comments

Sudo for Windows (2024)

https://github.com/microsoft/sudo
63•luispa•6h ago•41 comments

Changes in the system prompt between Claude Opus 4.6 and 4.7

https://simonwillison.net/2026/Apr/18/opus-system-prompt/
274•pretext•19h ago•163 comments

Swiss AI Initiative (2023)

https://www.swiss-ai.org
46•doener•7h ago•14 comments

Knitout and Kniterate 3

https://soup.agnescameron.info//2026/04/01/transfers.html
6•surprisetalk•3d ago•0 comments

A cache-friendly IPv6 LPM with AVX-512 (linearized B+-tree, real BGP benchmarks)

https://github.com/esutcu/planb-lpm
17•debugga•3h ago•4 comments

2,100 Swiss municipalities showing which provider handles their official email

https://mxmap.ch/
131•doener•7h ago•39 comments

Scientific datasets are riddled with copy-paste errors

https://www.sciencedetective.org/scientific-datasets-are-riddled-with-copy-paste-errors/
79•jruohonen•11h ago•19 comments

The RAM shortage could last years

https://www.theverge.com/ai-artificial-intelligence/914672/the-ram-shortage-could-last-years
258•omer_k•23h ago•287 comments

Prove you are a robot: CAPTCHAs for agents

https://browser-use.com/posts/prove-you-are-a-robot
84•lukasec•4d ago•42 comments

Six Levels of Dark Mode (2024)

https://cssence.com/2024/six-levels-of-dark-mode/
70•Akcium•11h ago•33 comments

Show HN: A lightweight way to make agents talk without paying for API usage

https://juanpabloaj.com/2026/04/16/a-lightweight-way-to-make-agents-talk-without-paying-for-api-u...
24•juanpabloaj•6h ago•6 comments

I wrote a CHIP-8 emulator in my own programming language

https://github.com/navid-m/chip8emu
63•pizza_man•11h ago•14 comments

Interesting Map Geometry and Mathematics

https://www.markrjohnsongames.com/2026/04/11/ultima-ratio-regum-0-11-update-57-interesting-map-ge...
22•Hooke•2d ago•0 comments

Nanopass Framework: Clean Compiler Creation Language

https://nanopass.org/
127•NordStreamYacht•4d ago•31 comments

A Common MVP Evolution: Service to System Integration to Product (2017)

https://www.skmurphy.com/blog/2017/08/07/a-common-evolution-service-to-system-integration-to-prod...
10•skmurphy•2d ago•2 comments

SPEAKE(a)R: Turn Speakers to Microphones for Fun and Profit [pdf] (2017)

https://www.usenix.org/system/files/conference/woot17/woot17-paper-guri.pdf
173•Eridanus2•21h ago•69 comments

Show HN: Faceoff – A terminal UI for following NHL games

https://www.vincentgregoire.com/faceoff/
109•vcf•12h ago•36 comments

Show HN: Prompt-to-Excalidraw demo with Gemma 4 E2B in the browser (3.1GB)

https://teamchong.github.io/turboquant-wasm/draw.html
109•teamchong•19h ago•45 comments

Notion leaks email addresses of all editors of any public page

https://twitter.com/weezerOSINT/status/2045849358462222720
355•Tiberium•15h ago•129 comments

4-bit floating point FP4

https://www.johndcook.com/blog/2026/04/17/fp4/
77•chmaynard•1d ago•56 comments