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•7mo ago

Comments

wahern•6mo 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•6mo 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•6mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•6mo 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•6mo 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•6mo 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•6mo ago
Learning such a language doesn’t mean I can use it.
o11c•6mo 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•6mo 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•6mo 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•6mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•6mo ago
It still is a thing, though.
cperciva•6mo 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•6mo 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.

Netflix to Acquire Warner Bros

https://about.netflix.com/en/news/netflix-to-acquire-warner-bros
1369•meetpateltech•11h ago•1084 comments

Cloudflare outage on December 5, 2025

https://blog.cloudflare.com/5-december-2025-outage/
503•meetpateltech•8h ago•364 comments

Gemini 3 Pro: the frontier of vision AI

https://blog.google/technology/developers/gemini-3-pro-vision/
288•xnx•7h ago•137 comments

Idempotency Keys for Exactly-Once Processing

https://www.morling.dev/blog/on-idempotency-keys/
64•defly•4d ago•28 comments

Perpetual Futures

https://www.bitsaboutmoney.com/archive/perpetual-futures-explained/
26•sirodoht•2h ago•7 comments

Frank Gehry has died

https://www.bbc.co.uk/news/articles/c5y2p22z9gno
84•ksajadi•2h ago•29 comments

Framework Sponsors CachyOS

https://discuss.cachyos.org/t/framework-sponsorship-for-cachyos/19376
119•d3Xt3r•3h ago•93 comments

Patterns for Defensive Programming in Rust

https://corrode.dev/blog/defensive-programming/
176•PaulHoule•7h ago•35 comments

Fizz Buzz in CSS

https://susam.net/fizz-buzz-in-css.html
43•froober•3h ago•8 comments

Most technical problems are people problems

https://blog.joeschrag.com/2023/11/most-technical-problems-are-really.html
295•mooreds•10h ago•236 comments

I'm Peter Roberts, immigration attorney who does work for YC and startups. AMA

152•proberts•7h ago•194 comments

Tides are weirder than you think

https://signoregalilei.com/2025/11/12/tides-are-weirder-than-you-think/
52•surprisetalk•4d ago•8 comments

Show HN: HCB Mobile – financial app built by 17 y/o, processing $6M/month

https://hackclub.com/fiscal-sponsorship/mobile/
86•mohamad08•2d ago•24 comments

Leaving Intel

https://www.brendangregg.com/blog//2025-12-05/leaving-intel.html
23•speckx•2h ago•3 comments

How fast can browsers process base64 data?

https://lemire.me/blog/2025/11/29/how-fast-can-browsers-process-base64-data/
9•mfiguiere•6d ago•2 comments

Why we built Lightpanda in Zig

https://lightpanda.io/blog/posts/why-we-built-lightpanda-in-zig
143•ashvardanian•5h ago•70 comments

Why are your models so big? (2023)

https://pawa.lt/braindump/tiny-models/
11•jxmorris12•3d ago•0 comments

Adenosine on the common path of rapid antidepressant action: The coffee paradox

https://genomicpress.kglmeridian.com/view/journals/brainmed/aop/article-10.61373-bm025c.0134/arti...
5•PaulHoule•1h ago•0 comments

Judge Signals Win for Software Freedom Conservancy in Vizio GPL Case

https://fossforce.com/2025/12/judge-signals-win-for-software-freedom-conservancy-in-vizio-gpl-case/
95•speckx•3h ago•8 comments

Onlook (YC W25) the Cursor for Designers Is Hiring a Founding Fullstack Engineer

1•D_R_Farrell•6h ago

Making RSS More Fun

https://matduggan.com/making-rss-more-fun/
169•salmon•10h ago•88 comments

UniFi 5G

https://blog.ui.com/article/introducing-unifi-5g
339•janandonly•16h ago•276 comments

A $20 drug in Europe requires a prescription and $800 in the U.S.

https://www.statnews.com/2025/10/31/why-miebo-costs-40-times-more-than-its-european-version/
147•geox•2h ago•138 comments

Compassionate Curmudgeon: Why we must root ourselves in the real world

https://theamericanscholar.org/compassionate-curmudgeon/
26•lermontov•3d ago•5 comments

Show HN: Kraa – Writing App for Everything

https://kraa.io/about
103•levmiseri•1d ago•59 comments

Nimony (Nim 3.0) Design Principles

https://nim-lang.org/araq/nimony.html
124•andsoitis•3d ago•80 comments

Framework Laptop 13 gets ARM processor with 12 cores via upgrade kit

https://www.notebookcheck.net/Framework-Laptop-13-gets-ARM-processor-with-12-cores-via-upgrade-ki...
259•woodrowbarlow•7h ago•128 comments

The Debug Adapter Protocol is a REPL protocol in disguise

https://zignar.net/2025/06/23/debug-adapter-protocol-is-a-repl-protocol/
17•Malp•4h ago•1 comments

Synadia and TigerBeetle Pledge $512k to the Zig Software Foundation

https://tigerbeetle.com/blog/2025-10-25-synadia-and-tigerbeetle-pledge-512k-to-the-zig-software-f...
153•cratermoon•7h ago•63 comments

The missing standard library for multithreading in JavaScript

https://github.com/W4G1/multithreading
9•W4G1•2h ago•0 comments