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

Comments

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

Bucketsquatting is (finally) dead

https://onecloudplease.com/blog/bucketsquatting-is-finally-dead
107•boyter•3h ago•46 comments

Willingness to look stupid

https://sharif.io/looking-stupid
392•Samin100•4d ago•131 comments

I traced $2B in grants and 45 states' lobbying behind age‑verification bills

https://old.reddit.com/r/linux/comments/1rshc1f/i_traced_2_billion_in_nonprofit_grants_and_45/
109•shaicoleman•1h ago•14 comments

Source code of Swedish e-government services has been leaked

https://darkwebinformer.com/full-source-code-of-swedens-e-government-platform-leaked-from-comprom...
65•tavro•1h ago•41 comments

Executing programs inside transformers with exponentially faster inference

https://www.percepta.ai/blog/can-llms-be-computers
137•u1hcw9nx•1d ago•32 comments

Peter Thiel's Antichrist Lectures

https://apnews.com/article/italy-peter-thiel-paypal-pope-vatican-c3a6c7d2daba501caf8152558ac2d743
36•aureliusm•1h ago•23 comments

Malus – Clean Room as a Service

https://malus.sh
1304•microflash•21h ago•474 comments

Show HN: fftool – A Terminal UI for FFmpeg – Shows Command Before It Runs

https://bensantora.com/posts/fftool-ffmpeg-tui-go/
10•taskset•1h ago•0 comments

Show HN: What was the world listening to? Music charts, 20 countries (1940–2025)

https://88mph.fm/
6•matteocantiello•2d ago•5 comments

“This is not the computer for you”

https://samhenri.gold/blog/20260312-this-is-not-the-computer-for-you/
526•MBCook•9h ago•211 comments

Ceno, browse the web without internet access

https://ceno.app/en/index.html?
37•mohsen1•5h ago•9 comments

Okmain: How to pick an OK main colour of an image

https://dgroshev.com/blog/okmain/
24•dgroshev•3d ago•1 comments

Vite 8.0 Is Out

https://vite.dev/blog/announcing-vite8
336•kothariji•7h ago•94 comments

Prefix sums at gigabytes per second with ARM NEON

https://lemire.me/blog/2026/03/08/prefix-sums-at-tens-of-gigabytes-per-second-with-arm-neon/
48•mfiguiere•4d ago•4 comments

ATMs didn’t kill bank teller jobs, but the iPhone did

https://davidoks.blog/p/why-the-atm-didnt-kill-bank-teller
436•colinprince•20h ago•453 comments

Bubble Sorted Amen Break

https://parametricavocado.itch.io/amen-sorting
344•eieio•18h ago•104 comments

Shall I implement it? No

https://gist.github.com/bretonium/291f4388e2de89a43b25c135b44e41f0
1320•breton•14h ago•483 comments

Enhancing gut-brain communication reversed cognitive decline in aging mice

https://med.stanford.edu/news/all-news/2026/03/gut-brain-cognitive-decline.html
317•mustaphah•19h ago•134 comments

IMG_0416 (2024)

https://ben-mini.com/2024/img-0416
106•TigerUniversity•3d ago•22 comments

An old photo of a large BBS (2022)

https://rachelbythebay.com/w/2022/01/26/swcbbs/
202•xbryanx•15h ago•134 comments

The Met releases high-def 3D scans of 140 famous art objects

https://www.openculture.com/2026/03/the-met-releases-high-definition-3d-scans-of-140-famous-art-o...
295•coloneltcb•19h ago•59 comments

Understanding the Go Runtime: The Scheduler

https://internals-for-interns.com/posts/go-runtime-scheduler/
121•valyala•3d ago•16 comments

Document poisoning in RAG systems: How attackers corrupt AI's sources

https://aminrj.com/posts/rag-document-poisoning/
124•aminerj•21h ago•50 comments

AI toys for children misread emotions and respond inappropriately

https://www.bbc.co.uk/news/articles/clyg4wx6nxgo
9•fredley•54m ago•1 comments

US private credit defaults hit record 9.2% in 2025, Fitch says

https://www.marketscreener.com/news/us-private-credit-defaults-hit-record-9-2-in-2025-fitch-says-...
375•JumpCrisscross•22h ago•424 comments

Grief and the AI split

https://blog.lmorchard.com/2026/03/11/grief-and-the-ai-split/
162•avernet•13h ago•251 comments

Never snooze a future

https://jacko.io/snooze.html
20•vinhnx•5d ago•6 comments

Celebrating Interesting Flickr Technologies

https://medium.com/@brightcarvings/celebrating-flickr-technology-3c93c8ddecc2
44•steerpike•1d ago•9 comments

Bringing Chrome to ARM64 Linux Devices

https://blog.chromium.org/2026/03/bringing-chrome-to-arm64-linux-devices.html
112•ingve•15h ago•56 comments

Worldwide Sidewalk Joy: Adding whimsy to neighborhoods

https://worldwidesidewalkjoy.com
24•NaOH•3d ago•3 comments