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

Comments

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

Humanoid Robot Actuators

https://www.firgelli.com/pages/humanoid-robot-actuators
102•ofrzeta•4h ago•22 comments

Using "underdrawings" for accurate text and numbers

https://samcollins.blog/underdrawings/
177•samcollins•2d ago•51 comments

BYOMesh – New LoRa mesh radio offers 100x the bandwidth

https://partyon.xyz/@nullagent/116499715071759135
349•nullagent•13h ago•113 comments

DeepClaude – Claude Code agent loop with DeepSeek V4 Pro

https://github.com/aattaran/deepclaude
405•alattaran•9h ago•149 comments

Midori, the first browser to offer a VPN with Mesh technology

https://astian.org/midori-en/performance-adblock-and-more-in-midori-11-7-1/
7•ponchale•1h ago•2 comments

Discovering hard disk physical geometry through microbenchmarking (2019)

https://blog.stuffedcow.net/2019/09/hard-disk-geometry-microbenchmarking/
65•TapamN•3d ago•3 comments

The 'Hidden' Costs of Great Abstractions

https://jdgr.net/the-hidden-costs-of-great-abstractions
148•jdgr•8h ago•51 comments

A Treasure Trove of Fossils Rewrites the Story of Early Life

https://www.quantamagazine.org/a-treasure-trove-of-cambrian-fossils-rewrites-the-story-of-early-l...
20•worldvoyageur•2d ago•0 comments

A desktop made for one

https://isene.org/2026/05/Audience-of-One.html
322•xngbuilds•16h ago•145 comments

Southwest Headquarters Tour

https://katherinemichel.github.io/blog/travel/southwest-headquarters-tour-2026.html
236•KatiMichel•14h ago•72 comments

OpenAI's o1 correctly diagnosed 67% of ER patients vs. 50-55% by triage doctors

https://www.theguardian.com/technology/2026/apr/30/ai-outperforms-doctors-in-harvard-trial-of-eme...
380•donsupreme•1d ago•324 comments

US–Indian space mission maps extreme subsidence in Mexico City

https://phys.org/news/2026-04-usindian-space-mission-extreme-subsidence.html
146•leopoldj•2d ago•59 comments

K3sup – bootstrap K3s over SSH in < 60s

https://github.com/alexellis/k3sup
46•rickcarlino•2d ago•13 comments

Texico: Learn the principles of programming without even touching a computer

https://www3.nhk.or.jp/nhkworld/en/shows/texico/
7•o4c•2d ago•0 comments

Tar Files Created on macOS Display Errors When Extracting on Linux (2024)

https://aruljohn.com/blog/macos-created-tar-files-linux-errors/
92•heresie-dabord•3d ago•64 comments

Denuvo has been cracked in all single-player games it previously protected

https://www.tomshardware.com/video-games/pc-gaming/denuvo-has-been-bypassed-in-all-single-player-...
318•oceansky•5d ago•191 comments

Introduction to Atom

https://validator.w3.org/feed/docs/atom.html
86•susam•9h ago•28 comments

Bad Connection: Global telecom exploitation by covert surveillance actors

https://citizenlab.ca/research/uncovering-global-telecom-exploitation-by-covert-surveillance-actors/
142•miohtama•15h ago•9 comments

New statue in London, attributed to Banksy, of a suited man, blinded by a flag

https://www.smithsonianmag.com/smart-news/attributed-to-banksy-a-new-statue-of-a-suited-man-blind...
367•dryadin•13h ago•323 comments

Mercedes-Benz commits to bringing back physical buttons

https://www.drive.com.au/news/mercedes-benz-commits-to-bringing-back-phycial-buttons/
680•teleforce•17h ago•384 comments

Stitch Together Lots of Little HTML Pages with Navigations for Interactions

https://blog.jim-nielsen.com/2026/small-html-pages/
29•OuterVale•3h ago•18 comments

Fun with polynomials and linear algebra; or, slight abstract nonsense

https://guille.site/posts/abstract-nonsense/
8•LolWolf•2d ago•0 comments

The text mode lie: why modern TUIs are a nightmare for accessibility

https://xogium.me/the-text-mode-lie-why-modern-tuis-are-a-nightmare-for-accessibility
199•SpyCoder77•7h ago•82 comments

Show HN: Ableton Live MCP

https://github.com/bschoepke/ableton-live-mcp
80•bschoepke•13h ago•52 comments

Text-to-CAD

https://github.com/earthtojake/text-to-cad
109•softservo•3d ago•30 comments

Why TUIs are back

https://wiki.alcidesfonseca.com/blog/why-tuis-are-back/
319•rickcarlino•13h ago•333 comments

I recreated the Apple Lisa computer inside an FPGA [video]

https://www.youtube.com/watch?v=8jNQDcpHc68
98•cyrc•14h ago•23 comments

Roger Sweet, Creator of the He-Man Action Figure, Dies at 91

https://www.nytimes.com/2026/04/29/arts/roger-sweet-dead-he-man.html
41•ChrisArchitect•2d ago•8 comments

Security through obscurity is not bad

https://mobeigi.com/blog/security/security-through-obscurity-is-not-bad/
152•mobeigi•17h ago•165 comments

Let's Buy Spirit Air

https://letsbuyspiritair.com/
310•bjhess•8h ago•300 comments