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.

John Carmack on the mistakes around Quake that ruined id software

https://twitter.com/ID_AA_Carmack/status/2069799283369345247
199•shadowtree•1h ago•80 comments

RubyLLM: A Ruby framework for all major AI providers

https://rubyllm.com/
170•doener•2h ago•22 comments

We’re making Bunny DNS free

https://bunny.net/blog/were-making-bunny-dns-free/
634•dabinat•8h ago•210 comments

CAPTCHAs have failed for 20 years

https://www.browserbase.com/blog/why-captchas-are-getting-harder
27•harsehaj•1h ago•19 comments

Show HN: Nub – A Bun-like all-in-one toolkit for Node.js

https://github.com/nubjs/nub
106•colinmcd•2h ago•23 comments

I taught a bucket to speak Git

https://www.tigrisdata.com/blog/objgit/
21•xena•1h ago•2 comments

PR spam today looks like email spam in the early 2000s

https://www.greptile.com/blog/prs-on-openclaw
30•dakshgupta•2h ago•20 comments

For Most of the World, Open-Source AI Is the Only Way Forward

https://techstrong.ai/articles/for-most-of-the-world-open-source-ai-is-the-only-way-forward/
37•CrankyBear•2h ago•3 comments

Running Windows Games on a Hobby OS with Wine

https://astral-os.org/posts/2026/04/03/wine-on-astral.html
44•avaliosdev•2h ago•11 comments

Krea 2: SOTA open-weights 12B image model

https://www.krea.ai/blog/krea-2-technical-report
166•mattnewton•1d ago•21 comments

A Practical Guide to SSH Tunnels: Local and Remote Port Forwarding

https://labs.iximiuz.com/tutorials/ssh-tunnels
152•signa11•4d ago•30 comments

Genuinely, my all-time favourite image: Mamenchisaurus hochuanensis

https://svpow.com/2026/06/04/genuinely-my-all-time-favourite-image-mamenchisaurus-hochuanensis/
41•surprisetalk•2d ago•11 comments

Boffin claims Microsoft's "quantum leap" is invalid due to "basic Python errors"

https://www.theregister.com/research/2026/06/24/boffin-claims-microsofts-supposed-quantum-leap-do...
65•connorboyle•1h ago•28 comments

Show HN: Monolisa v3 – a typeface for developers and creatives

https://www.monolisa.dev/
85•bebraw•2d ago•18 comments

Haystack: Open-Source AI Framework for Production Ready Agents, RAG

https://haystack.deepset.ai/
60•doener•5h ago•19 comments

Founding a company in Germany: €9600, 152 days and I still can't send an invoice

https://paolino.me/founding-a-company-in-germany/
430•earcar•4h ago•495 comments

Show HN: Pure Effect – Reproduce production bugs on your laptop without a DB

https://pure-effect.org
30•tie-in•2d ago•6 comments

Stealing Is a Skill

https://ben-mini.com/2026/stealing-is-a-skill
109•bewal416•4h ago•76 comments

Edsger Dijkstra's Library (Housed and Archived in Leuven, Belgium)

https://www.dijkstrascry.com/inventory
19•rramadass•1h ago•2 comments

Raspberry Pi Pico W as USB Wi-Fi Adapter

https://gitlab.com/baiyibai/pico-usb-wifi
229•byb•13h ago•108 comments

Systems optimization should be part of CI/CD

https://ucbskyadrs.github.io/blog/levi/
19•ttanv•4h ago•2 comments

Statistics that live in your SQL

https://kolistat.com/blog/the-stats-duck-v0-6-0/
111•caerbannogwhite•2d ago•15 comments

OpenAI and Broadcom unveil LLM-optimized inference chip

https://openai.com/index/openai-broadcom-jalapeno-inference-chip/
111•meetpateltech•3h ago•36 comments

Ashby (YC W19) Is Hiring EMEA Engineers Who Can Design

https://www.ashbyhq.com/careers?ashby_jid=87b96eef-edc1-4de4-adb6-d460126d02f8&utm_source=hn
1•abhikp•10h ago

Quebec town recognizes trees as living beings with rights

https://www.cbc.ca/news/canada/montreal/terrasse-vaudreil-quebec-tree-rights-9.7243634
65•speckx•1h ago•57 comments

Minimus container images are now free

https://images.minimus.io/
101•dimastopel•5h ago•59 comments

"Fix" MacBook Neo Cursor Lag: Record 1 Pixel of the Screen Every 10 Seconds

https://gist.github.com/retroplasma/ec21767d0a8380c7ea9c2fbee1c7d6bf
190•retroplasma•14h ago•78 comments

François Englert (1932 – 2026)

https://home.cern/francois-englert-1932-2026/
51•toomuchtodo•3d ago•3 comments

Too many R packages: CRAN is inundated with submissions

https://rworks.dev/posts/too-many-R-packages/
74•ionychal•6h ago•58 comments

Show HN: peerd – AI agent harness that runs entirely in your browser

https://github.com/NotASithLord/peerd
10•NotASithLord•1d ago•2 comments