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

Comments

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

Say No to Palantir in Europe

https://action.wemove.eu/sign/2026-03-palantir-petition-EN
372•Betelbuddy•2h ago•73 comments

The bot situation on the internet is worse than you could imagine

https://gladeart.com/blog/the-bot-situation-on-the-internet-is-actually-worse-than-you-could-imag...
28•ohjeez•38m ago•6 comments

Voyager 1 runs on 69 KB of memory and an 8-track tape recorder

https://techfixated.com/a-1977-time-capsule-voyager-1-runs-on-69-kb-of-memory-and-an-8-track-tape...
28•speckx•44m ago•12 comments

Overestimation of microplastics potentially caused by scientists' gloves

https://news.umich.edu/nitrile-and-latex-gloves-may-cause-overestimation-of-microplastics-u-m-stu...
379•giuliomagnifico•7h ago•163 comments

App that shows real-time lightning on Earth is showing bombings in Middle East

https://maps.blitzortung.org/
44•0ut0flin3•1h ago•6 comments

Stop Publishing Garbage Data, It's Embarrassing

https://successfulsoftware.net/2026/03/29/stop-publishing-garbage-data-its-embarrassing/
28•hermitcrab•1h ago•26 comments

Miasma: A tool to trap AI web scrapers in an endless poison pit

https://github.com/austin-weeks/miasma
184•LucidLynx•6h ago•133 comments

Police used AI facial recognition to wrongly arrest TN woman for crimes in ND

https://www.cnn.com/2026/03/29/us/angela-lipps-ai-facial-recognition
108•ourmandave•2h ago•47 comments

Founder of GitLab battles cancer by founding companies

https://sytse.com/cancer/
1267•bob_theslob646•23h ago•245 comments

Technology: The (nearly) perfect USB cable tester does exist

https://blog.literarily-starved.com/2026/02/technology-the-nearly-perfect-usb-cable-tester-does-e...
194•birdculture•3d ago•88 comments

The RISE RISC-V Runners: free, native RISC-V CI on GitHub

https://riseproject.dev/2026/03/24/announcing-the-rise-risc-v-runners-free-native-risc-v-ci-on-gi...
7•thebeardisred•3d ago•0 comments

LinkedIn uses 2.4 GB RAM across two tabs

270•hrncode•7h ago•188 comments

Show HN: Create a full language server in Go with 3.17 spec support

https://github.com/owenrumney/go-lsp
53•rumno0•4d ago•11 comments

AI overly affirms users asking for personal advice

https://news.stanford.edu/stories/2026/03/ai-advice-sycophantic-models-research
729•oldfrenchfries•1d ago•577 comments

I turned my Kindle into my own personal newspaper

https://manualdousuario.net/en/how-to-kindle-personal-newspaper/
127•rpgbr•2d ago•45 comments

The Failure of the Thermodynamics of Computation(2010)

https://sites.pitt.edu/~jdnorton/Goodies/Idealization/index.html
31•nill0•2d ago•1 comments

Full network of clitoral nerves mapped out for first time

https://www.theguardian.com/society/2026/mar/29/full-network-clitoral-nerves-mapped-out-first-tim...
25•onei•1h ago•5 comments

CSS is DOOMed

https://nielsleenheer.com/articles/2026/css-is-doomed-rendering-doom-in-3d-with-css/
442•msephton•20h ago•105 comments

Comparison shows audiophiles waste a lot of money

https://www.tomshardware.com/pc-components/sound-cards/comparison-of-usd4-000-boutique-audio-cabl...
14•nick__m•50m ago•18 comments

Siclair Microvision (1977)

https://r-type.org/articles/art-452.htm
42•joebig•2d ago•16 comments

Figma's MCP Update Reflects a Larger Industry Shift

https://metedata.substack.com/p/a-small-figma-update-and-a-big-signal
14•young_mete•1h ago•10 comments

Alzheimer's disease mortality among taxi and ambulance drivers (2024)

https://www.bmj.com/content/387/bmj-2024-082194
189•bookofjoe•16h ago•126 comments

OpenBSD on Motorola 88000 Processors

http://miod.online.fr/software/openbsd/stories/m88k1.html
134•rbanffy•2d ago•20 comments

I decompiled the White House's new app

https://thereallo.dev/blog/decompiling-the-white-house-app
592•amarcheschi•1d ago•216 comments

Nonfiction Publishing, Under Threat, Is More Important

https://newrepublic.com/article/207659/non-fiction-publishing-threat-important-ever
36•Hooke•3d ago•25 comments

Further human + AI + proof assistant work on Knuth's "Claude Cycles" problem

https://twitter.com/BoWang87/status/2037648937453232504
243•mean_mistreater•22h ago•161 comments

A Verilog to Factorio Compiler and Simulator (Working RISC-V CPU)

https://github.com/ben-j-c/verilog2factorio
132•signa11•3d ago•16 comments

I Built an Open-World Engine for the N64 [video]

https://www.youtube.com/watch?v=lXxmIw9axWw
434•msephton•1d ago•76 comments

Building a Mostly IPv6 Only Home Network

https://varunpriolkar.com/2026/03/building-a-mostly-ipv6-only-home-network/
50•arhue•4d ago•59 comments

What if AI doesn't need more RAM but better math?

https://adlrocha.substack.com/p/adlrocha-what-if-ai-doesnt-need-more
139•adlrocha•8h ago•74 comments