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.

GrapheneOS has been ported to Android 17

https://discuss.grapheneos.org/d/36469-grapheneos-has-been-ported-to-android-17-and-official-rele...
786•Cider9986•13h ago•388 comments

Running local models is good now

https://vickiboykis.com/2026/06/15/running-local-models-is-good-now/
1346•jfb•19h ago•511 comments

Show HN: High-Res Neural Cellular Automata

https://cells2pixels.github.io/
12•esychology•53m ago•0 comments

Hacker News but for Independent Blogs

https://bubbles.town/
79•headalgorithm•2h ago•26 comments

Humiliating IIS servers for fun and jail time

https://mll.sh/humiliating-iis-servers-for-fun-and-jail-time/
273•denysvitali•11h ago•63 comments

The founder's playbook: Building an AI-native startup

https://claude.com/blog/the-founders-playbook
85•e2e4•3h ago•85 comments

Subterranean fungi networks more than 100 quadrillion km in length

https://www.theguardian.com/science/2026/jun/11/arbuscular-mycorrhizal-fungi-plant-life-climate-g...
73•tosh•5d ago•17 comments

TIL: You can make HTTP requests without curl using Bash /dev/TCP

https://mareksuppa.com/til/bash-dev-tcp-http-without-curl/
423•mrshu•17h ago•196 comments

Wolfram Language and Mathematica version 15

https://writings.stephenwolfram.com/2026/06/launching-version-15-of-wolfram-language-mathematica-...
171•alok-g•11h ago•83 comments

Calvin and Hobbes and the price of integrity

https://therepublicofletters.substack.com/p/calvin-and-hobbes-and-the-price-of
430•pseudolus•18h ago•184 comments

Has AI already killed self-help nonfiction books?

https://tim.blog/2026/06/12/has-ai-already-killed-nonfiction/
308•imakwana•17h ago•340 comments

GPT‑NL: a sovereign language model for the Netherlands

https://www.tno.nl/en/digital/artificial-intelligence/gpt-nl/
214•root-parent•16h ago•215 comments

Stop Using JWTs

https://gist.github.com/samsch/0d1f3d3b4745d778f78b230cf6061452
400•dzonga•17h ago•229 comments

U.S. Science Is in Chaos

https://www.scientificamerican.com/article/americas-compact-between-science-and-politics-is-broken/
13•presspot•28m ago•1 comments

Stop Killing Games fails to secure EU law despite 1.3M signatures

https://www.dexerto.com/gaming/stop-killing-games-fails-to-secure-eu-law-despite-1-3m-signatures-...
250•slymax•8h ago•149 comments

Semiclassical Gravity Efficiently Solves NP-Complete Problems

https://arxiv.org/abs/2606.14806
30•ascarshen•6h ago•13 comments

Map Clustering Is Not My Favorite

https://blog.greg.technology/2026/06/12/map-clustering-is-not-my-favorite.html
9•gregsadetsky•4d ago•2 comments

SpaceX to buy Cursor for $60B

https://www.reuters.com/legal/transactional/spacex-buy-anysphere-60-billion-2026-06-16/
1034•itsmarcelg•23h ago•1530 comments

Lattice Triangles Are Rare

https://axiommath.ai/territory/the-reveal
20•skogstokig•6d ago•3 comments

Making 'food out of thin air' (2024)

https://www.noemamag.com/making-food-out-of-thin-air/
21•muchweight•2d ago•4 comments

But yak shaving is fun (2019)

https://parksb.github.io/en/article/32.html
266•parksb•19h ago•76 comments

From Chesterton's fence to Chesterton's gap

https://stephantul.github.io/blog/unfence/
8•stephantul•3h ago•7 comments

A brief tour of the PDP-11, the most influential minicomputer of all time (2022)

https://arstechnica.com/gadgets/2022/03/a-brief-tour-of-the-pdp-11-the-most-influential-minicompu...
80•jensgk•2d ago•32 comments

The Amphibious Villagers of Indonesia

https://www.economist.com/interactive/1843/2026/06/12/the-amphibious-villagers-of-indonesia
28•haritha-j•2d ago•8 comments

10Gb/s Ethernet: switching to a Broadcom SFP+ module

https://www.gilesthomas.com/2026/06/10g-ethernet-switching-to-broadcom-sfp-plus
149•gpjt•16h ago•133 comments

Qwen-Robot Suite: A Foundation Model Suite for Physical World Intelligence

https://qwen.ai/blog?id=qwen-robotsuite
181•ilreb•21h ago•31 comments

Ask HN: What has been bothering you lately?

4•julienreszka•41m ago•3 comments

A Nipkow Disk Mechanical TV Simulator

https://analogtv.net/mechanical-lab
48•ambanmba•2d ago•6 comments

Show HN: cuTile Rust: Safe, data-race-free GPU kernels in Rust

https://github.com/nvlabs/cutile-rs
83•melihelibol•14h ago•14 comments

Chameleon Ultra: a flashdrive sized NFC toolkit

https://github.com/RfidResearchGroup/ChameleonUltra
13•elisaado•2d ago•7 comments