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.

Marble Fountain

https://willmorrison.net/posts/marble-fountain/
513•chris_overseas•10h ago•57 comments

Ask HN: What Are You Working On? (Nov 2025)

137•david927•5h ago•405 comments

Montana becomes first state to enshrine 'right to compute' into law

https://montananewsroom.com/montana-becomes-first-state-to-enshrine-right-to-compute-into-law/
349•bilsbie•13h ago•177 comments

Drilling down on Uncle Sam's proposed TP-Link ban

https://krebsonsecurity.com/2025/11/drilling-down-on-uncle-sams-proposed-tp-link-ban/
128•todsacerdoti•8h ago•123 comments

JVM exceptions are weird: a decompiler perspective

https://purplesyringa.moe/blog/jvm-exceptions-are-weird-a-decompiler-perspective/
78•vrnvu•5d ago•15 comments

Itiner-e: the Google Maps of Roman Roads

https://itiner-e.org/
7•helsinkiandrew•16h ago•0 comments

How the UK lost its shipbuilding industry

https://www.construction-physics.com/p/how-the-uk-lost-its-shipbuilding
8•surprisetalk•1h ago•6 comments

My Git history was a mess of 'update' and 'fix' – so I made AI clean it up

https://github.com/f/git-rewrite-commits
14•fka•1h ago•27 comments

The Manuscripts of Edsger W. Dijkstra

https://www.cs.utexas.edu/~EWD/
186•nathan-barry•11h ago•72 comments

Show HN: DroidDock – A sleek macOS app for browsing Android device files via ADB

https://rajivm1991.github.io/DroidDock/
13•rajivm1991•1h ago•3 comments

Building a 2.5kWh battery from disposable vapes to power my workshop [video]

https://www.youtube.com/watch?v=dy-wFixuRVU
119•rsanek•6d ago•72 comments

The Principles of Diffusion Models

https://arxiv.org/abs/2510.21890
143•Anon84•10h ago•14 comments

Work after work: Notes from an unemployed new grad watching the job market break

https://urlahmed.com/2025/11/05/work-after-work-notes-from-an-unemployed-new-grad-watching-the-jo...
154•linkregister•1h ago•103 comments

Metabolic and cellular differences between sedentary and active individuals

https://howardluksmd.substack.com/p/if-youre-not-active-youre-sick-you
89•rzk•4h ago•51 comments

The Sega Master System

https://bumbershootsoft.wordpress.com/2025/11/08/the-sega-master-system/
58•ibobev•7h ago•18 comments

How to maintain good vision amidst the myopia epidemic

https://ssathe.substack.com/p/vision-in-the-digital-age
20•plun9•1h ago•23 comments

Iran faces unprecedented drought as water crisis hits Tehran

https://www.bbc.com/news/articles/cy4p2yzmem0o
22•FridayoLeary•1h ago•22 comments

Bumble Berry Pi – A Cheap DIY Raspberry Pi Handheld Cyberdeck

https://github.com/samcervantes/bumble-berry-pi
107•MakerSam•9h ago•20 comments

Sued by Nintendo

https://www.suedbynintendo.com/
52•notepad0x90•2h ago•7 comments

Reviving Classic Unix Games: A 20-Year Journey Through Software Archaeology

https://vejeta.com/reviving-classic-unix-games-a-20-year-journey-through-software-archaeology/
133•mwheeler•13h ago•51 comments

Zensical – A modern static site generator built by the Material for MkDocs team

https://squidfunk.github.io/mkdocs-material/blog/2025/11/05/zensical/
125•japhyr•13h ago•42 comments

CHIP8 – emulator, assembler, game, vhdl hardware implementations

http://blog.dominikrudnik.pl/chip8-emulator-assembler-game-vhdl
55•qikcik•6d ago•3 comments

The Computer Church – Pennsylvania Computer and Technology Museum

https://www.thecomputerchurch.org/
39•gregsadetsky•6h ago•8 comments

Email verification protocol

https://github.com/WICG/email-verification-protocol
132•sgoto•1w ago•96 comments

When your hash becomes a string: Hunting Ruby's million-to-one memory bug

https://mensfeld.pl/2025/11/ruby-ffi-gc-bug-hash-becomes-string/
91•phmx•5d ago•44 comments

Visualize FastAPI endpoints with FastAPI-Voyager

https://www.newsyeah.fun/voyager/
108•tank-34•14h ago•18 comments

Using bubblewrap to add sandboxing to NetBSD

https://blog.netbsd.org/tnf/entry/gsoc2025_bubblewrap_sandboxing
82•jaypatelani•13h ago•22 comments

Show HN: Trilogy Studio, open-source browser-based SQL editor and visualizer

https://trilogydata.dev/trilogy-studio-core/#screen=dashboard-import&import=https%3A%2F%2Ftrilogy...
5•efromvt•3h ago•1 comments

Today I Learned: Binfmt_misc

https://dfir.ch/posts/today_i_learned_binfmt_misc/
6•malmoeb•6d ago•2 comments

Solving Every Sudoku Puzzle (2006)

https://norvig.com/sudoku.html
32•djoldman•5d ago•6 comments