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.

Voyager 1 Is About to Reach One Light-Day from Earth

https://scienceclock.com/voyager-1-is-about-to-reach-one-light-day-from-earth/
480•ashishgupta2209•5h ago•163 comments

A Fast 64-Bit Date Algorithm (30–40% faster by counting dates backwards)

https://www.benjoffe.com/fast-date-64
103•benjoffe•3d ago•26 comments

From blood sugar to brain relief: GLP-1 therapy slashes migraine frequency

https://www.medlink.com/news/from-blood-sugar-to-brain-relief-glp-1-therapy-slashes-migraine-freq...
66•Anon84•3h ago•39 comments

Gemini CLI Tips and Tricks for Agentic Coding

https://github.com/addyosmani/gemini-cli-tips
38•ayoisaiah•1h ago•9 comments

API that auto-routes to the cheapest AI provider (OpenAI/Anthropic/Gemini)

https://tokensaver.org/
4•h2o_wine•32m ago•1 comments

A cell so minimal that it challenges definitions of life

https://www.quantamagazine.org/a-cell-so-minimal-that-it-challenges-definitions-of-life-20251124/
187•ibobev•9h ago•81 comments

A Woman on a Mission to Photograph Every Species of Hummingbird

https://www.audubon.org/magazine/meet-woman-mission-photograph-every-species-of-hummingbird-world
27•zeech•3d ago•2 comments

Show HN: I turned algae into a bio-altimeter and put it on a weather balloon

https://radi8.dev/blog/stratospore/
57•radeeyate•4d ago•5 comments

Statistical Process Control in Python

https://timothyfraser.com/sigma/statistical-process-control-in-python.html
164•lifeisstillgood•11h ago•52 comments

OpenAI needs to raise at least $207B by 2030

https://ft.com/content/23e54a28-6f63-4533-ab96-3756d9c88bad
447•akira_067•4h ago•385 comments

Optery (YC W22) Hiring CISO, Release Manager, Tech Lead (Node), Full Stack Eng

https://www.optery.com/careers/
1•beyondd•2h ago

DRAM prices are spiking, but I don't trust the industry's why

https://www.xda-developers.com/dram-prices-spiking-dont-trust-industry-reasons/
72•binarycrusader•2h ago•31 comments

JOPA: Java compiler in C++, Jikes modernized to Java 6 with Claude

https://github.com/7mind/jopa
35•pshirshov•3d ago•25 comments

China Has Three Reusable Rockets Ready for Their Debut Flights

https://www.china-in-space.com/p/china-has-three-reusable-rockets
21•speckx•59m ago•7 comments

Show HN: KiDoom – Running DOOM on PCB Traces

https://www.mikeayles.com/#kidoom
305•mikeayles•21h ago•41 comments

Copyparty, the FOSS file server [video]

https://www.youtube.com/watch?v=15_-hgsX2V0
190•franczesko•6d ago•48 comments

Surprisingly, Emacs on Android is pretty good

https://kristofferbalintona.me/posts/202505291438/
211•harryday•3d ago•108 comments

Is DWPD Still a Useful SSD Spec?

https://klarasystems.com/articles/is-dwpd-still-useful-ssd-spec/
40•zdw•5d ago•26 comments

Scaleway turns Mac Minis into high‑density, Raspberry Pi–managed servers

https://www.scaleway.com/en/blog/how-we-turn-apples-mac-mini-into-high-performance-dedicated-serv...
64•Lwrless•2h ago•70 comments

Image Diffusion Models Exhibit Emergent Temporal Propagation in Videos

https://arxiv.org/abs/2511.19936
89•50kIters•11h ago•12 comments

Qiskit open-source SDK for working with quantum computers

https://github.com/Qiskit/qiskit
28•thinkingemote•7h ago•2 comments

Slop Detective – Fight the Slop Syndicate

https://slopdetective.kagi.com/
35•speckx•3h ago•13 comments

Jakarta is now the biggest city in the world

https://www.axios.com/2025/11/24/jakarta-tokyo-worlds-biggest-city-population
422•skx001•1d ago•319 comments

CS234: Reinforcement Learning Winter 2025

https://web.stanford.edu/class/cs234/
176•jonbaer•19h ago•43 comments

Show HN: We built an open source, zero webhooks payment processor

https://github.com/flowglad/flowglad
352•agreeahmed•1d ago•201 comments

How to repurpose your old phone into a web server

https://far.computer/how-to/
310•louismerlin•4d ago•106 comments

A new bridge links the math of infinity to computer science

https://www.quantamagazine.org/a-new-bridge-links-the-strange-math-of-infinity-to-computer-scienc...
227•digital55•23h ago•127 comments

Launch HN: Onyx (YC W24) – Open-source chat UI

223•Weves•1d ago•144 comments

1,700-year-old Roman sarcophagus is unearthed in Budapest

https://apnews.com/article/hungary-roman-sarcophagus-discovery-budapest-77a41fe190bbcc167b43d0514...
129•gmays•1d ago•71 comments

I DM'd a Korean presidential candidate and ended up building his core campaign

https://medium.com/@wjsdj2008/i-dmd-a-korean-presidential-candidate-and-ended-up-building-his-cor...
143•wjsdj2009•6h ago•69 comments