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.

John Deere owners will get the right to repair equipment under FTC settlement

https://apnews.com/article/john-deere-right-to-repair-agriculture-equipment-cb7514ffedb95c130a976...
188•djoldman•1h ago•43 comments

Separating signal from noise in coding evaluations

https://openai.com/index/separating-signal-from-noise-coding-evaluations/
143•sk4rekr0w•4h ago•59 comments

Chatto is now open source

https://www.hmans.dev/blog/chatto-is-open-source
711•speckx•9h ago•190 comments

Remote Attestation

https://www.liamcvw.com/p/remote-attestation
9•lcvw•34m ago•3 comments

We made Grok 4.5, GPT-5.5, and Claude build the same apps

https://www.tryai.dev/blog/grok-4.5-vs-gpt-5.5-vs-claude-build-off
26•hershyb_•1h ago•6 comments

Mistral's Robostral Navigate: a state of the art robotics navigation model

https://mistral.ai/news/robostral-navigate/
406•ottomengis•10h ago•95 comments

Cloudflare Drop

https://www.cloudflare.com/drop/
225•coloneltcb•5h ago•116 comments

Show HN: Microsoft releases Flint, a visualization language for AI agents

https://microsoft.github.io/flint-chart/#/
192•chenglong-hn•7h ago•72 comments

Unicode's transliteration rules are Turing-complete

https://seriot.ch/computation/uts35/
18•beefburger•15h ago•2 comments

Grok 4.5

https://x.ai/news/grok-4-5
457•BoumTAC•7h ago•551 comments

Turning a pile of documents into a searchable useable knowledge base

https://github.com/linuxrebel/DocuBrowser
67•linuxrebe1•4h ago•9 comments

Show HN: Yamanote.fun – A complete soundscape for Tokyo's Yamanote line

https://www.yamanote.fun/
46•madebymagnolia•1d ago•12 comments

FAANG Simulator

https://www.abeyk.com/escape-the-rat-race/
262•nerdbiscuits•5h ago•104 comments

GPT‑Live

https://openai.com/index/introducing-gpt-live/
587•logickkk1•8h ago•399 comments

Beyond Git: Real-Time Version Control for Godot – Lilith Duncan – GodotCon 2026 [video]

https://www.youtube.com/watch?v=CAJ_iIedx_I
8•surprisetalk•6d ago•1 comments

A bug which affected only left handed users

https://shkspr.mobi/blog/2026/07/a-bug-which-only-affected-left-handed-users/
78•sixhobbits•11h ago•42 comments

DKIM2 and DMARCbis Have Landed

https://stalw.art/blog/dkim2-dmarcbis/
73•StalwartLabs•2d ago•50 comments

Rewriting Bun in Rust

https://bun.com/blog/bun-in-rust
251•afturner•3h ago•135 comments

TypeScript 7

https://devblogs.microsoft.com/typescript/announcing-typescript-7-0/
456•DanRosenwasser•9h ago•170 comments

New Sweden: the US's long-lost 'secret' colony

https://www.bbc.com/travel/article/20260629-new-sweden-the-uss-long-lost-secret-colony
38•bookofjoe•5h ago•2 comments

MIRA: Multiplayer Interactive World Models Trained on Rocket League

https://mira-wm.com/
4•ethanlipson•39m ago•0 comments

My road trip with the do-gooding cactus smugglers

https://economist.com/1843/2026/03/06/my-road-trip-with-the-do-gooding-cactus-smugglers
13•andsoitis•3d ago•1 comments

Decoding the obfuscated bash script on a Uniqlo t-shirt

https://tris.sherliker.net/blog/obfuscated-self-evaluating-bash-script-by-cdn-akamai-being-suppli...
1291•speerer•16h ago•206 comments

Show HN: Frugon – Find which LLM calls a cheaper model could handle (local, MIT)

https://github.com/Rodiun/frugon
7•jarodrh•1d ago•1 comments

OpenMandriva: Statement regarding attempted distribution sabotage

https://forum.openmandriva.org/t/statement-regarding-attempted-distribution-sabotage/8997
71•workethics•6h ago•13 comments

Cloudflare Meerkat - Globally distributed consensus

https://blog.cloudflare.com/meerkat-introduction/
213•bobnamob•11h ago•44 comments

Open Source Barware: free, local-first bar inventory software (GPLv3)

https://opensourcebarware.com
18•RichBJamison•3h ago•11 comments

EU now one step away from reviving private message scanning rules

https://cyberinsider.com/eu-now-one-step-away-from-reviving-private-message-scanning-rules/
354•ggirelli•8h ago•135 comments

I Built a Telegram Client for Pi

https://www.npmjs.com/package/@atharva-again/pi-tg
52•atharva-again•2d ago•24 comments

SWE-1.7 Reach Near GPT 5.5 and Opus Intelligence

https://cognition.com/blog/swe-1-7
251•mekpro•8h ago•128 comments