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•6mo 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.

It’s time to free JavaScript

https://javascript.tm/letter
158•pavelai•3h ago•60 comments

PGlite – Embeddable Postgres

https://pglite.dev/
159•dsego•2h ago•43 comments

Ghostty is now non-profit

https://mitchellh.com/writing/ghostty-non-profit
1171•vrnvu•18h ago•248 comments

Valve reveals it’s the architect behind a push to bring Windows games to Arm

https://www.theverge.com/report/820656/valve-interview-arm-gaming-steamos-pierre-loup-griffais
819•evolve2k•1d ago•710 comments

Elites could shape mass preferences as AI reduces persuasion costs

https://arxiv.org/abs/2512.04047
240•50kIters•4h ago•244 comments

Reverse engineering a $1B Legal AI tool exposed 100k+ confidential files

https://alexschapiro.com/security/vulnerability/2025/12/02/filevine-api-100k
723•bearsyankees•19h ago•246 comments

Programming peaked

https://functional.computer/blog/programming-peaked
23•Antibabelic•2h ago•8 comments

Show HN: Walrus – a Kafka alternative written in Rust

https://github.com/nubskr/walrus
38•janicerk•2d ago•14 comments

I ignore the spotlight as a staff engineer

https://lalitm.com/software-engineering-outside-the-spotlight/
8•todsacerdoti•1h ago•0 comments

Interop and MathML Core

https://conflor.es/blog/2025-11-27-interop-and-mathml/
16•todsacerdoti•3d ago•1 comments

Building optimistic UI in Rails (and learn custom elements)

https://railsdesigner.com/custom-elements/
12•amalinovic•1h ago•0 comments

Unreal Tournament 2004 is back

https://old.reddit.com/r/unrealtournament/comments/1pdbe69/breaking_unreal_tournament_2004_is_back/
123•keithoffer•2h ago•47 comments

Micron Announces Exit from Crucial Consumer Business

https://investors.micron.com/news-releases/news-release-details/micron-announces-exit-crucial-con...
621•simlevesque•18h ago•306 comments

Uncloud - Tool for deploying containerised apps across servers without k8s

https://uncloud.run/
182•rgun•6h ago•82 comments

Average DRAM price in USD over last 18 months

https://pcpartpicker.com/trends/price/memory/
365•zekrioca•12h ago•262 comments

Show HN: MTXT – Music Text Format

https://github.com/Daninet/mtxt
12•daninet•4d ago•2 comments

30 years ago today "Netscape and Sun announce JavaScript"

https://web.archive.org/web/20070916144913/http://wp.netscape.com/newsref/pr/newsrelease67.html
28•donohoe•1h ago•6 comments

Saturn (YC S24) Is Hiring Senior AI Engineer

https://www.ycombinator.com/companies/saturn/jobs/R9s9o5f-senior-ai-engineer
1•etticat•5h ago

1D Conway's Life glider found, 3.7B cells long

https://conwaylife.com/forums/viewtopic.php?&p=222136#p222136
466•nooks•19h ago•163 comments

RCE Vulnerability in React and Next.js

https://github.com/vercel/next.js/security/advisories/GHSA-9qr9-h5gf-34mp
552•rayhaanj•20h ago•207 comments

Show HN: I built a dashboard to compare mortgage rates across 120 credit unions

https://finfam.app/blog/credit-union-mortgages
269•mhashemi•16h ago•90 comments

All the Way Down

https://www.futilitycloset.com/2025/11/17/all-the-way-down-2/
27•surprisetalk•5d ago•10 comments

Why WinQuake exists and how it works

https://fabiensanglard.net/winquake/index.html
105•wicket•10h ago•12 comments

Kea DHCP: Modern, open source DHCPv4 and DHCPv6 server

https://www.isc.org/kea/
99•doener•12h ago•31 comments

Acme, a brief history of one of the protocols which has changed the Internet

https://blog.brocas.org/2025/12/01/ACME-a-brief-history-of-one-of-the-protocols-which-has-changed...
125•coffee--•13h ago•65 comments

8086 Microcode Browser

https://nand2mario.github.io/posts/2025/8086_microcode_browser/
125•zdw•15h ago•1 comments

Show HN: A Minimal Monthly Task Planner (printable, offline, no signup)

https://printcalendar.top/
66•defcc•7h ago•23 comments

NRC Completes Safety Review of TerraPower Natrium [pdf]

https://www.nrc.gov/sites/default/files/cdn/doc-collection-news/2025/25-063.pdf
4•mpweiher•2h ago•0 comments

Launch HN: Phind 3 (YC S22) – Every answer is a mini-app

117•rushingcreek•19h ago•87 comments

How to Synthesize a House Loop

https://loopmaster.xyz/tutorials/how-to-synthesize-a-house-loop
241•stagas•6d ago•87 comments