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

Comments

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

Deutsche Telekom is violating Net Neutrality

https://netzbremse.de/en/
157•tietjens•2h ago•69 comments

This paper has been cited more than 6k times. It's fatally flawed.

https://statmodeling.stat.columbia.edu/2026/01/22/aking/
58•timr•1h ago•7 comments

Google confirms 'high-friction' sideloading flow is coming to Android

https://www.androidauthority.com/google-sideloading-android-high-friction-process-3633468/
222•_____k•5d ago•128 comments

BirdyChat becomes first European chat app that is interoperable with WhatsApp

https://www.birdy.chat/blog/first-to-interoperate-with-whatsapp
603•joooscha•15h ago•356 comments

Adoption of EVs tied to real-world reductions in air pollution: study

https://keck.usc.edu/news/adoption-of-electric-vehicles-tied-to-real-world-reductions-in-air-poll...
391•hhs•10h ago•334 comments

Jurassic Park - Tablet device on Nedry's desk? (2012)

https://www.therpf.com/forums/threads/jurassic-park-tablet-device-on-nedrys-desk.169883/
14•exvi•1h ago•1 comments

A Lament for Aperture

https://ikennd.ac/blog/2026/01/old-man-yells-at-modern-software-design/
90•firloop•4d ago•20 comments

David Patterson: Challenges and Research Directions for LLM Inference Hardware

https://arxiv.org/abs/2601.05047
65•transpute•7h ago•3 comments

German economists push for gold repatriation from U.S. vaults

https://seekingalpha.com/news/4542254-german-economists-push-for-gold-repatriation-from-us-vaults
66•saubeidl•1h ago•44 comments

Two Weeks Until Tapeout

https://essenceia.github.io/projects/two_weeks_until_tapeout/
117•client4•9h ago•6 comments

Intrinsically stretchable 2D MoS2 transistors

https://www.nature.com/articles/s41467-026-68504-2
8•bookofjoe•4d ago•0 comments

BU-808: How to Prolong Lithium-based Batteries (2023)

https://www.batteryuniversity.com/article/bu-808-how-to-prolong-lithium-based-batteries/
5•eswat•2d ago•0 comments

Introduction to PostgreSQL Indexes

https://dlt.github.io/blog/posts/introduction-to-postgresql-indexes/
8•dlt•2h ago•0 comments

Show HN: AutoShorts – Local, GPU-accelerated AI video pipeline for creators

https://github.com/divyaprakash0426/autoshorts
16•divyaprakash•3h ago•5 comments

Postmortem: Our first VLEO satellite mission (with imagery and flight data)

https://albedo.com/post/clarity-1-what-worked-and-where-we-go-next
176•topherhaddad•14h ago•58 comments

Claude Code's new hidden feature: Swarms

https://twitter.com/NicerInPerson/status/2014989679796347375
417•AffableSpatula•20h ago•286 comments

I built a 2x faster lexer, then discovered I/O was the real bottleneck

https://modulovalue.com/blog/syscall-overhead-tar-gz-io-performance/
24•modulovalue•4d ago•7 comments

Typography on Pencils (2023)

https://www.presentandcorrect.com/blogs/blog/typography-on-pencils-1-5
74•NaOH•4d ago•6 comments

We X-Rayed a Suspicious FTDI USB Cable

https://eclypsium.com/blog/xray-counterfeit-usb-cable/
148•aa_is_op•10h ago•55 comments

Raspberry Pi Drag Race: Pi 1 to Pi 5 – Performance Comparison

https://the-diy-life.com/raspberry-pi-drag-race-pi-1-to-pi-5-performance-comparison/
175•verginer•16h ago•81 comments

Second Win11 emergency out of band update to address disastrous Patch Tuesday

https://www.windowscentral.com/microsoft/windows-11/windows-11-second-emergency-out-of-band-updat...
160•speckx•7h ago•101 comments

Memory layout in Zig with formulas

https://raymondtana.github.io/math/programming/2026/01/23/zig-alignment-and-sizing.html
117•raymondtana•18h ago•25 comments

Hands-On with Two Apple Network Server Prototype ROMs

http://oldvcr.blogspot.com/2026/01/hands-on-with-two-apple-network-server.html
3•todsacerdoti•2h ago•0 comments

Show HN: Sightline – Shodan-style search for real-world infra using OSM Data

https://github.com/ni5arga/sightline
4•ni5arga•2h ago•0 comments

Like digging 'your own grave': The translators grappling with losing work to AI

https://www.cnn.com/2026/01/23/tech/translation-language-jobs-ai-automation-intl
54•myk-e•2h ago•40 comments

Nvidia-smi hangs indefinitely after ~66 days

https://github.com/NVIDIA/open-gpu-kernel-modules/issues/971
171•tosh•7h ago•38 comments

Ask HN: Gmail spam filtering suddenly marking everything as spam?

174•goopthink•18h ago•113 comments

Small Kafka: Tansu and SQLite on a free t3.micro

https://blog.tansu.io/articles/broker-aws-free-tier
90•rmoff•4d ago•19 comments

Maze Algorithms (2017)

http://www.jamisbuck.org/mazes/
134•surprisetalk•1d ago•31 comments

Poland's energy grid was targeted by never-before-seen wiper malware

https://arstechnica.com/security/2026/01/wiper-malware-targeted-poland-energy-grid-but-failed-to-...
232•Bender•13h ago•108 comments