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•1y ago

Comments

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

Postmortem: TanStack NPM supply-chain compromise

https://tanstack.com/blog/npm-supply-chain-compromise-postmortem
788•varunsharma07•11h ago•297 comments

Screenshots of Old Desktop OSes

http://www.typewritten.org/Media/
88•adunk•2h ago•22 comments

They Live (1988) inspired Adblocker

https://github.com/davmlaw/they_live_adblocker
187•tokenburner•7h ago•49 comments

If AI writes your code, why use Python?

https://medium.com/@NMitchem/if-ai-writes-your-code-why-use-python-bf8c4ba1a055
436•indigodaddy•11h ago•465 comments

UCLA discovers first stroke rehabilitation drug to repair brain damage (2025)

https://stemcell.ucla.edu/news/ucla-discovers-first-stroke-rehabilitation-drug-repair-brain-damage
317•bookofjoe•14h ago•65 comments

Extremely Low Frequencies

https://computer.rip/2026-05-09-extremely-low-frequencies.html
55•pinewurst•4h ago•1 comments

Claude Platform on AWS

https://claude.com/blog/claude-platform-on-aws
123•matrixhelix•6h ago•56 comments

Software Internals Book Club

https://eatonphil.com/bookclub.html
82•aragonite•5h ago•13 comments

Rtwatch: Watch videos with friends using WebRTC

https://github.com/pion/rtwatch
8•nateb2022•2d ago•0 comments

Google says criminal hackers used AI to find a major software flaw

https://www.nytimes.com/2026/05/11/us/politics/google-hackers-attack-ai.html
173•donohoe•18h ago•130 comments

A lost ancient script reveals how writing as we know it began

https://www.newscientist.com/article/2524042-a-lost-ancient-script-reveals-how-writing-as-we-know...
60•emot•4d ago•27 comments

I let AI build a tool to help me figure out what was waking me up at night

https://martin.sh/i-let-ai-build-a-tool-to-help-me-figure-out-what-was-waking-me-up-at-night/
163•showmypost•11h ago•166 comments

Show HN: A modern Music Player Daemon based on Rockbox firmware

https://github.com/tsirysndr/rockbox-zig
72•tsiry•2d ago•13 comments

I hate soldering

https://user8.bearblog.dev/rant/
92•James72689•4d ago•78 comments

Nullsoft, 1997-2004 (2004)

https://slate.com/technology/2004/11/the-death-of-the-last-maverick-tech-company.html
271•downbad_•4d ago•78 comments

Boriel BASIC

https://zxbasic.readthedocs.io/en/docs/
28•AlexeyBrin•2d ago•7 comments

Library for fast mapping of Java records to native memory

https://github.com/mamba-studio/TypedMemory
137•joe_mwangi•12h ago•30 comments

Interaction Models

https://thinkingmachines.ai/blog/interaction-models/
198•smhx•11h ago•25 comments

GitLab announces workforce reduction and end of their CREDIT values

https://about.gitlab.com/blog/gitlab-act-2/
479•AnonGitLabEmpl•11h ago•477 comments

When semiconductor materials misbehave

https://semiengineering.com/when-semiconductor-materials-misbehave/
9•PaulHoule•3d ago•0 comments

VGA Memory Access Is Complicated

https://www.os2museum.com/wp/learn-something-old-every-day-part-xxi-vga-memory-access-is-complica...
49•ingve•2d ago•6 comments

Show HN: TikTok but for scientific papers

https://andreaturchet.github.io/website/index.html
114•ciwrl•16h ago•55 comments

The rise and fall of snake oil

https://www.historytoday.com/archive/history-matters/rise-and-fall-snake-oil
64•samizdis•4d ago•33 comments

Interfaze: A new model architecture built for high accuracy at scale

https://interfaze.ai/blog/interfaze-a-new-model-architecture-built-for-high-accuracy-at-scale
137•yoeven•15h ago•34 comments

Griffin PowerMate driver for modern macOS

https://github.com/jameslockman/Griffin-PowerMate-Driver
73•classichasclass•10h ago•26 comments

Training an LLM in Swift, Part 1: Taking matrix mult from Gflop/s to Tflop/s

https://www.cocoawithlove.com/blog/matrix-multiplications-swift.html
233•zdw•1d ago•12 comments

High-precision HDC reference instrument for the Sol Star System

https://pypi.org/project/ephemerides-spectral/
10•lemonforest•2d ago•1 comments

Silverback Imfura took a chance, and ended up alone

https://gorillafund.org/mountain-gorillas/silverback-imfura-took-a-chance-and-ended-up-alone/
57•alex000kim•2d ago•20 comments

Abstract Machines for Logic Programs

https://chrisistyping.bearblog.dev/abstract-machines-for-logic-programs/
42•surprisetalk•2d ago•2 comments

AMÁLIA and the future of European Portuguese LLMs

https://duarteocarmo.com/blog/amalia-and-the-future-of-european-portuguese-llms
134•johnbarron•3d ago•63 comments