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•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.

YouTube to automatically label AI-generated videos

https://blog.youtube/news-and-events/improving-ai-labels-viewers-creators/
928•nopg•15h ago•565 comments

A Eureka machine that thinks like nature and explores what AI cannot

https://iisc.ac.in/a-eureka-machine-that-thinks-like-nature-and-explores-what-ai-cannot/
80•kunalsin9h•4h ago•28 comments

I analysed 20 years of my chats

https://drobinin.com/posts/am-i-a-bad-friend/
152•valzevul•11h ago•52 comments

I think Anthropic and OpenAI have found product-market fit

https://simonwillison.net/2026/May/27/product-market-fit/
893•simonw•18h ago•1014 comments

Hallucinate – Massively Multiplayer Online Rave

https://hallucinate.site
225•stagas•7h ago•94 comments

SimCity 3k in 4k (2025)

https://www.thran.uk/writ/hdid/2025/12/simcity-3k-in-4k.html
379•speckx•17h ago•154 comments

What Apple and Google are doing to push notifications

https://www.jacquescorbytuech.com/writing/what-apple-and-google-are-doing-your-push-notifications
299•iamacyborg•15h ago•302 comments

Rapira (Рапира) – Soviet programming language interpreter

https://github.com/begoon/rapira
24•begoon•3d ago•13 comments

I'm Getting into Mesh Networks (Meshtastic, MeshCore, and Reticulum)

https://www.jonaharagon.com/posts/im-getting-into-mesh-networks-meshtastic-meshcore-and-reticulum/
214•Panda_•15h ago•84 comments

The Ask

https://randsinrepose.com/archives/the-ask/
85•digitallogic•2d ago•51 comments

More Whimsical OEIS Sequences

https://www.jeremykun.com/shortform/2026-05-22-1528/
10•surprisetalk•1d ago•1 comments

Rust (and Slint) on a Jailbroken Kindle

https://sverre.me/blog/rust-on-kindle/
170•homarp•15h ago•22 comments

DuckDuckGo search saw 28% more visits after Google said people love AI mode

https://www.pcgamer.com/hardware/duckduckgos-ai-free-search-saw-nearly-28-percent-more-visits-in-...
888•HelloUsername•18h ago•425 comments

AMD pulls a bait-and-switch on Linux users with Vivado licensing changes

https://itsfoss.com/news/amd-vivado-bait-and-switch-on-linux-users/
5•teleforce•16m ago•0 comments

A New Typst Template for Pandoc (2025)

https://imaginarytext.ca/posts/2025/typst-templates-for-pandoc/
83•ankitg12•2d ago•14 comments

Seeing Around Corners Using Smartphone-Grade Lidar

https://spectrum.ieee.org/smartphone-grade-lidar
12•marc__1•2d ago•1 comments

FBI Arrests CIA Official with $40M in Gold Bars in His Home

https://www.nytimes.com/2026/05/27/us/politics/fbi-arrest-cia-official-gold-bars.html
350•cwwc•11h ago•236 comments

Incident with Pull Requests, Issues, Git Operations and API Requests

https://www.githubstatus.com/incidents/xy1tt3hs572m
308•maxnoe•22h ago•200 comments

Go: Support for Generic Methods

https://github.com/golang/go/issues/77273
253•f311a•1d ago•216 comments

Warm up your MacBook (2019)

https://z3ugma.github.io/2019/11/18/warm-up-your-macbook/
84•kristianp•14h ago•84 comments

Investigating how prompt politeness affects LLM accuracy (2025)

https://arxiv.org/abs/2510.04950
79•KnuthIsGod•2d ago•76 comments

Biff is a command line datetime Swiss army knife

https://github.com/BurntSushi/biff
33•burntsushi•8h ago•22 comments

RamAIn (YC W26) Is Hiring

https://www.ycombinator.com/companies/ramain/jobs/hqvmyKN-founding-gtm-engineer
1•svee•8h ago

AI sticker shock hits corporate America

https://www.axios.com/2026/05/28/ai-spending-roi-enterprise-costs
5•1vuio0pswjnm7•34m ago•0 comments

Google employee charged with $1M Polymarket insider trading bet on search term

https://www.cnbc.com/2026/05/27/google-employee-polymarket-insider-trading.html
192•pseudolus•10h ago•116 comments

Stress disrupts hippocampal integration of overlapping events, memory inference

https://www.science.org/doi/10.1126/sciadv.aea5496?user_id=66c4bf745d78644b3aa57b08
129•gmays•18h ago•20 comments

Mini Micro Fantasy Computer

https://miniscript.org/MiniMicro/index.html#about
257•nicoloren•1d ago•82 comments

Zero Lines Maze: What the 8-Bit Guy's One-Liner Can Still Teach Us

https://retrogamecoders.com/zero-lines-maze/
52•ibobev•1d ago•18 comments

My new obsession: A horse-racing board game of pure luck

https://alexanderbjoy.com/horse-race-board-game/
83•surprisetalk•2d ago•53 comments

Can we have the day off?

https://mlsu.io/posts/day-off/
1104•mlsu•10h ago•643 comments