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.

Anonymous GitHub account mass-dropping undisclosed 0-days

https://github.com/bikini/exploitarium
305•binyu•3h ago•127 comments

OpenRA

https://www.openra.net/
357•tosh•6h ago•74 comments

DSpark: Speculative decoding accelerates LLM inference [pdf]

https://github.com/deepseek-ai/DeepSpec/blob/main/DSpark_paper.pdf
662•aurenvale•9h ago•249 comments

AI Is Designing Radio Chips That Humans Couldn't Even Imagine

https://spectrum.ieee.org/ai-radio-chip-design
29•Brajeshwar•3d ago•10 comments

Suspicious Discontinuities (2020)

https://danluu.com/discontinuities/
128•tosh•4h ago•36 comments

Fintech Engineering Handbook

https://w.pitula.me/fintech-engineering-handbook/
346•signa11•7h ago•112 comments

Post-Mythos Cybersecurity: Keep calm and carry on

https://cephalosec.com/blog/cybersecurity-in-the-post-mythos-era-keep-calm-and-carry-on/
70•Versipelle•3h ago•22 comments

Zuckerberg's Increasingly Bizarre War on Whistleblowers

https://pluralistic.net/2026/06/27/zuckerstreisand-2/
344•HotGarbage•3h ago•126 comments

Supabase (YC S20) Is Hiring for Multigres

https://jobs.ashbyhq.com/supabase/2e718684-4f75-4a99-8d6b-3b6bd44e4228
1•awalias•1h ago

One man, two kernels, and a lot of RISC-V

https://www.theregister.com/software/2026/06/26/one-man-two-kernels-and-a-lot-of-risc-v/5262858
38•LorenDB•1d ago•4 comments

If you can't hold it, you don't own it

https://dervis.de/physical/
234•cemdervis•6h ago•155 comments

Reducing tick density along recreational trails in Ottawa, Canada

https://www.sciencedirect.com/science/article/pii/S1877959X26000476
63•bushwart•2d ago•22 comments

Previewing GPT‑5.6 Sol: a next-generation model

https://openai.com/index/previewing-gpt-5-6-sol/
1088•minimaxir•1d ago•692 comments

How Many Elementary Particles Are There, Really?

https://www.quantamagazine.org/how-many-elementary-particles-are-there-really-20260615/
82•rwmj•5h ago•63 comments

Long Wave radio era set to end with switch-off

https://www.economist.com/britain/2026/06/25/the-bbc-switches-off-its-oldest-service
134•edward•1d ago•119 comments

Researchers have developed pixels that can emit and analyse light together

https://ethz.ch/en/news-and-events/eth-news/news/2026/06/a-new-type-of-pixel.html
18•tspng•10h ago•11 comments

Task Failed Successfully: Saturating NIC and Disk Bandwidth

https://blog.mrcroxx.com/posts/task-failed-successfully-saturating-nic-and-disk-bandwidth/
27•MrCroxx•4d ago•8 comments

Linux on Older Hardware: The Complete Revival Guide

https://www.fosslinux.com/158206/linux-on-older-hardware-revival-guide.htm
160•tapanjk•2d ago•91 comments

Beer CSS – Build material design in record time

https://www.beercss.com
108•Seb-C•9h ago•55 comments

The US Army Issued Ocarinas to Soldiers in World War II

https://www.flutetunes.com/articles/my-flute-goes-to-war/
97•tomcam•2d ago•56 comments

Streaming services' obnoxiously loud ads become illegal on July 1 in California

https://arstechnica.com/gadgets/2026/06/streaming-services-obnoxiously-loud-ads-become-illegal-on...
164•speckx•5h ago•37 comments

Why does kinetic energy increase quadratically, not linearly, with speed? (2011)

https://physics.stackexchange.com/questions/535/why-does-kinetic-energy-increase-quadratically-no...
323•ProxyTracer•19h ago•172 comments

WordStar: A Writer's Word Processor (1996)

https://www.sfwriter.com/wordstar.htm
154•droidjj•14h ago•77 comments

Doctors suspected man had brain cancer. He had worms

https://arstechnica.com/health/2026/06/doctors-suspected-man-had-brain-cancer-he-actually-had-worms/
26•Bender•1h ago•10 comments

Underarm Bowling Incident of 1981

https://en.wikipedia.org/wiki/Underarm_bowling_incident_of_1981
106•EndXA•3d ago•87 comments

Faster KNN search in Manticore: 2-pass HNSW, batched distances, and AVX-512

https://medium.com/@s_nikolaev/faster-knn-search-in-manticore-2-pass-hnsw-batched-distances-and-a...
50•snikolaev•1d ago•2 comments

Anatomy of a Failed (Nation-State?) Attack

https://grack.com/blog/2026/06/25/dissecting-a-failed-nation-state-attack/
128•signa11•15h ago•26 comments

Cultures of Making and Relating

https://blog.khinsen.net/posts/2026/06/25/cultures.html
35•akkartik•2d ago•2 comments

Jest/Vitest interactive course (runs in the browser)

https://howtotestfrontend.com/courses/jest-vitest-fundamentals
45•howToTestFE•2d ago•11 comments

MicroVMs: Run isolated sandboxes with full lifecycle control

https://aws.amazon.com/blogs/aws/run-isolated-sandboxes-with-full-lifecycle-control-aws-lambda-in...
369•justincormack•4d ago•199 comments