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•8mo ago

Comments

wahern•8mo 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•8mo 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•8mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•8mo 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•8mo 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•8mo 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•8mo ago
Learning such a language doesn’t mean I can use it.
o11c•8mo 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•8mo 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•8mo 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•8mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•8mo ago
It still is a thing, though.
cperciva•8mo 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•8mo 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.

Kiel Institute Analysis: US Americans pay 96% of tariff burden

https://www.kielinstitut.de/publications/americas-own-goal-who-pays-the-tariffs-19398/
550•47282847•1h ago•424 comments

CSS Web Components for marketing sites (2024)

https://hawkticehurst.com/2024/11/css-web-components-for-marketing-sites/
37•zigzag312•2h ago•13 comments

The Microstructure of Wealth Transfer in Prediction Markets

https://www.jbecker.dev/research/prediction-market-microstructure
40•jonbecker•1h ago•18 comments

GLM-4.7-Flash

https://huggingface.co/zai-org/GLM-4.7-Flash
185•scrlk•2h ago•49 comments

Apple testing new App Store design that blurs the line between ads and results

https://9to5mac.com/2026/01/16/iphone-apple-app-store-search-results-ads-new-design/
77•ksec•1h ago•31 comments

Iterative image reconstruction using random cubic bézier strokes

https://tangled.org/luthenwald.tngl.sh/splined
29•luthenwald•4d ago•4 comments

Show HN: Pipenet – A Modern Alternative to Localtunnel

https://pipenet.dev/
15•punkpeye•1h ago•4 comments

Folding NASA Experience into an Origamist's Toolkit

https://spinoff.nasa.gov/Folding_NASA_Experience_into_an_Origamist%E2%80%99s_Toolkit
46•andsoitis•2d ago•4 comments

A decentralized peer-to-peer messaging application that operates over Bluetooth

https://bitchat.free/
446•no_creativity_•10h ago•260 comments

Radboud University selects Fairphone as standard smartphone for employees

https://www.ru.nl/en/staff/news/radboud-university-selects-fairphone-as-standard-smartphone-for-e...
418•ardentsword•9h ago•189 comments

Luxury Yacht is a desktop app for managing Kubernetes clusters

https://github.com/luxury-yacht/app
35•mooreds•4d ago•11 comments

What it's like to be banned from the US for fighting online hate

https://www.technologyreview.com/2026/01/19/1131384/what-its-like-to-be-banned-from-the-us-for-fi...
32•HotGarbage•44m ago•7 comments

Dead Internet Theory

https://kudmitry.com/articles/dead-internet-theory/
560•skwee357•21h ago•616 comments

Cows Can Use Sophisticated Tools

https://nautil.us/the-far-side-had-it-all-wrong-cows-really-can-use-sophisticated-tools-1262026/
15•Tomte•54m ago•2 comments

I set all 376 Vim options and I'm still a fool

https://evanhahn.com/i-set-all-376-vim-options-and-im-still-a-fool/
49•todsacerdoti•2d ago•9 comments

Robust Conditional 3D Shape Generation from Casual Captures

https://facebookresearch.github.io/ShapeR/
24•lastdong•5h ago•1 comments

Ask HN: COBOL devs, how are AI coding affecting your work?

114•zkid18•4h ago•118 comments

Show HN: I quit coding years ago. AI brought me back

https://calquio.com/finance/compound-interest
282•ivcatcher•16h ago•376 comments

Nepal's Mountainside Teahouses Elevate the Experience for Trekkers

https://www.smithsonianmag.com/travel/nepal-mountainside-teahouses-elevate-experience-trekkers-he...
84•bookofjoe•4d ago•34 comments

Flux 2 Klein pure C inference

https://github.com/antirez/flux2.c
397•antirez•23h ago•130 comments

Raccoons break into liquor stores, scale skyscrapers and pick locks

https://theconversation.com/raccoons-break-into-liquor-stores-scale-skyscrapers-and-pick-locks-st...
8•pseudolus•29m ago•0 comments

Provide agents with automated feedback

https://banay.me/dont-waste-your-backpressure/
159•ghuntley•2d ago•80 comments

Wikipedia: WikiProject AI Cleanup

https://en.wikipedia.org/wiki/Wikipedia:WikiProject_AI_Cleanup
193•thinkingemote•7h ago•72 comments

AVX-512: First Impressions on Performance and Programmability

https://shihab-shahriar.github.io//blog/2026/AVX-512-First-Impressions-on-Performance-and-Program...
118•shihab•5d ago•46 comments

Gladys West's vital contributions to GPS technology

https://en.wikipedia.org/wiki/Gladys_West
61•hackernj•2d ago•5 comments

Amazon is ending all inventory commingling as of March 31, 2026

https://twitter.com/ghhughes/status/2012824754319753456
386•MrBuddyCasino•5h ago•208 comments

Gas Town Decoded

https://www.alilleybrinker.com/mini/gas-town-decoded/
190•alilleybrinker•4d ago•200 comments

The Code-Only Agent

https://rijnard.com/blog/the-code-only-agent
130•emersonmacro•15h ago•56 comments

West Midlands police chief quits over AI hallucination

https://www.theregister.com/2026/01/19/copper_chief_cops_it_after/
97•YeGoblynQueenne•2h ago•69 comments

Gaussian Splatting – A$AP Rocky "Helicopter" music video

https://radiancefields.com/a-ap-rocky-releases-helicopter-music-video-featuring-gaussian-splatting
726•ChrisArchitect•1d ago•239 comments