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

Comments

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

Show HN: The HN Arcade

https://andrewgy8.github.io/hnarcade/
27•yuppiepuppie•35m ago•8 comments

There's only one Woz, but we can all learn from him

https://www.fastcompany.com/91477114/steve-wozniak-woz-apple-the-tech-interactive-humanitarian-award
122•coloneltcb•4d ago•47 comments

ASML staffing changes could result in a net reduction of around 1700 positions

https://www.asml.com/en/news/press-releases/2026/strengthening-focus-on-engineering-and-innovation
229•dep_b•3h ago•202 comments

SVG Path Editor

https://yqnn.github.io/svg-path-editor/
99•gurjeet•5d ago•8 comments

Prism

https://openai.com/index/introducing-prism
659•meetpateltech•17h ago•386 comments

A few random notes from Claude coding quite a bit last few weeks

https://twitter.com/karpathy/status/2015883857489522876
651•bigwheels•1d ago•530 comments

Rust at Scale: An Added Layer of Security for WhatsApp

https://engineering.fb.com/2026/01/27/security/rust-at-scale-security-whatsapp/
53•ubj•5h ago•8 comments

Thirty Years of the Square Kilometre Array

https://physicsworld.com/a/thirty-years-of-the-square-kilometre-array-heres-what-the-worlds-large...
18•mooreds•2d ago•4 comments

430k-year-old well-preserved wooden tools are the oldest ever found

https://www.nytimes.com/2026/01/26/science/archaeology-neanderthals-tools.html
428•bookofjoe•19h ago•225 comments

Pandas 3.0

https://pandas.pydata.org/community/blog/pandas-3.0.html
52•jonbaer•4d ago•9 comments

Golden Ratio using an equilateral triangle inscribed in a circle

https://geometrycode.com/free/how-to-graphically-derive-the-golden-ratio-using-an-equilateral-tri...
85•peter_d_sherman•4d ago•22 comments

Rust’s Standard Library on the GPU

https://www.vectorware.com/blog/rust-std-on-gpu/
189•justaboutanyone•4d ago•30 comments

Make.ts

https://matklad.github.io/2026/01/27/make-ts.html
81•ingve•3h ago•46 comments

Doing the thing is doing the thing

https://www.softwaredesign.ing/blog/doing-the-thing-is-doing-the-thing
410•prakhar897•1d ago•131 comments

Parametric CAD in Rust

https://campedersen.com/vcad
176•ecto•14h ago•118 comments

Lennart Poettering, Christian Brauner founded a new company

https://amutable.com/about
306•hornedhob•16h ago•449 comments

Xfwl4 – The Roadmap for a Xfce Wayland Compositor

https://alexxcons.github.io/blogpost_15.html
325•pantalaimon•22h ago•243 comments

Time Station Emulator

https://github.com/kangtastic/timestation
180•FriedPickles•14h ago•45 comments

Devuan – Debian Without Systemd

https://www.devuan.org/
9•smartmic•2h ago•8 comments

Amazon closing its Fresh and Go stores

https://finance.yahoo.com/news/amazon-closing-fresh-grocery-convenience-150437789.html
239•trenning•19h ago•453 comments

AI2: Open Coding Agents

https://allenai.org/blog/open-coding-agents
195•publicmatt•18h ago•30 comments

Show HN: One Human + One Agent = One Browser From Scratch in 20K LOC

https://emsh.cat/one-human-one-agent-one-browser/
249•embedding-shape•22h ago•116 comments

SoundCloud Data Breach Now on HaveIBeenPwned

https://haveibeenpwned.com/Breach/SoundCloud
181•gnabgib•18h ago•95 comments

FBI is investigating Minnesota Signal chats tracking ICE

https://www.nbcnews.com/tech/internet/fbi-investigating-minnesota-signal-minneapolis-group-ice-pa...
773•duxup•17h ago•1057 comments

Australian high schoolers build coding platform to help learners in Sri Lanka

https://www.abc.net.au/news/2026-01-21/qld-teenagers-create-coding-app-code-lab-sri-lanka-student...
10•thadusTeam•1h ago•1 comments

Notes on starting to use Django

https://jvns.ca/blog/2026/01/27/some-notes-on-starting-to-use-django/
105•ingve•12h ago•40 comments

I found the perfect yearly calendar (for me)

https://blog.notmyhostna.me/posts/i-found-the-perfect-yearly-calendar-for-me
71•dewey•4d ago•27 comments

Extremophile molds are invading art museums

https://www.scientificamerican.com/article/how-extremophile-molds-are-destroying-museum-artifacts/
110•sohkamyung•4d ago•53 comments

Show HN: LemonSlice – Upgrade your voice agents to real-time video

101•lcolucci•17h ago•100 comments

AISLE’s autonomous analyzer found all CVEs in the January OpenSSL release

https://aisle.com/blog/aisle-discovered-12-out-of-12-openssl-vulnerabilities
168•mmsc•9h ago•121 comments