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.

Exapunks (2018)

https://www.zachtronics.com/exapunks/
163•yu3zhou4•2h ago•56 comments

Since Linux 6.9, LUKS suspend stopped wiping disk-encryption keys from memory

https://mathstodon.xyz/@iblech/116769502749142438
340•IngoBlechschmid•6h ago•164 comments

EFF letter to FTC on X consent order (2 July 2026) [pdf]

https://cdn.arstechnica.net/wp-content/uploads/2026/07/EFF-letter-to-FTC-on-X-consent-order-7-2-2...
55•Terretta•2h ago•12 comments

Lightning Memory-Mapped Database Manager (LMDB) 1.0

http://www.lmdb.tech/doc/
32•radiator•1h ago•15 comments

Virginia Bans Sale of Geolocation Data

https://www.hunton.com/privacy-and-cybersecurity-law-blog/virginia-bans-sale-of-geolocation-data
9•toomuchtodo•29m ago•2 comments

Podman v6.0.0

https://blog.podman.io/2026/07/introducing-podman-v6-0-0/
257•soheilpro•7h ago•98 comments

PeerTube is a free, decentralized and federated video platform

https://github.com/Chocobozzz/PeerTube
413•doener•10h ago•176 comments

Vulkan is now available on NetBSD

https://github.com/segaboy/vulkan-netbsd
45•segaboy81•2h ago•12 comments

Postgres transactions are a distributed systems superpower

https://www.dbos.dev/blog/co-locating-workflow-state-with-your-data
51•KraftyOne•2h ago•18 comments

How to ask for help from people who don't know you

https://pradyuprasad.com/writings/how-to-ask-for-help/
296•FigurativeVoid•8h ago•42 comments

JEP 539: Strict Field Initialization in the JVM moved to preview

https://openjdk.org/jeps/539
36•za3faran•2h ago•12 comments

Launch HN: Manufact (YC S25) – MCP Cloud

https://manufact.com
89•pzullo•6h ago•59 comments

Spain Orders Blacklist of Palantir from Public and Private Companies

https://clashreport.com/world/articles/spain-orders-blacklist-of-us-tech-giant-palantir-from-publ...
449•mgh2•6h ago•137 comments

Claude-real-video - any LLM can watch a video

https://github.com/HUANGCHIHHUNGLeo/claude-real-video
23•cortexosmain•2h ago•3 comments

AI can't be listed as inventor on patent applications, Japan's top court rules

https://japannews.yomiuri.co.jp/science-nature/technology/20260306-314930/
328•mushstory•7h ago•176 comments

The Short Leash AI Coding Method for Beating Fable

https://blog.okturtles.org/2026/07/short-leash-ai-method/
14•Riseed•2h ago•1 comments

Ask HN: Since when does Craigslist's front page have emojis?

24•argee•1d ago•27 comments

German button maker searched rivers of American Midwest for valuable shells

https://www.smithsonianmag.com/smithsonian-institution/how-one-german-button-maker-searched-the-r...
121•bookofjoe•4d ago•39 comments

Modeling the Covid-19 Outbreak with J (2020)

https://datakinds.github.io//2020/03/15/modeling-the-coronavirus-outbreak-with-j
16•surprisetalk•2d ago•0 comments

Is One Layer Enough? A Single Transformer Layer Matches Full-Parameter RL Train

https://arxiv.org/abs/2607.01232
127•tcp_handshaker•9h ago•29 comments

Show HN: CLI tool for detecting non-exact code duplication with embedding models

https://github.com/rafal-qa/slopo
69•rkochanowski•7h ago•31 comments

Hazel (YC W24) Is Hiring for Our Largest Government Contract

https://www.ycombinator.com/companies/hazel-2/jobs/3epPWgu-full-stack-engineer-ts-sci
1•augustschen•8h ago

Android Developer Verification: Threat masquerading as protection

https://f-droid.org/2026/07/01/adv-malware.html
1528•drewfax•18h ago•639 comments

The Egg Bandits Made a Thousand Times the Fine They Just Paid for Price Fixing

https://www.thebignewsletter.com/p/crime-pays-the-egg-bandits-made-a
400•toomuchtodo•8h ago•183 comments

The fall of the theorem economy

https://davidbessis.substack.com/p/the-fall-of-the-theorem-economy
234•varjag•13h ago•101 comments

How VictoriaLogs Stores Your Logs in a Columnar Layout

https://victoriametrics.com/blog/victorialogs-internals-columnar-storage-on-disk/index.html
41•eatonphil•4d ago•6 comments

A New Catalog of Stellar Rotation Periods for over a Million Stars

https://aasnova.org/2026/07/01/a-new-catalog-of-stellar-rotation-periods-for-over-a-million-stars/
7•visha1v•2h ago•1 comments

Wireless LAN SD

https://www.sdcard.org/developers/sd-standard-overview/sdio-isdio/wireless-lan-sd/
10•sharpshadow•1h ago•5 comments

The primary purpose of code review is to find code that will be hard to maintain

https://mathstodon.xyz/@mjd/115096720350507897
299•ColinWright•9h ago•156 comments

Show HN: Bramble – Local-first password manager

https://github.com/flythenimbus/bramble
5•MegagramEnjoyer•2h ago•0 comments