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.

Show HN: The same nine streaming subscriptions cost $702/year more than in 2021

https://honestlyranked.com/guides/streaming-price-increases/
86•honestlyranked•1h ago•80 comments

What algorithm did Windows XP use to choose your initial user picture?

https://devblogs.microsoft.com/oldnewthing/20260909-00/?p=112683
94•soheilpro•2h ago•38 comments

DeepSeek v4.1 Flash

https://twitter.com/deepseek_ai/status/2097930608790167907
460•Liwink•5h ago•244 comments

Stockfish 19

https://stockfishchess.org/blog/2026/stockfish-19/
76•atiedebee•2d ago•40 comments

iPhone Duo

https://www.apple.com/iphone-duo/
1238•thecosmicfrog•17h ago•2168 comments

Show HN: What if the speed of light was 5 km/h?

https://rivendell.dmitrybrant.com/relativity/
388•dmitrybrant•9h ago•160 comments

Mathematicians want proof OpenAI didn't use their work

https://www.theverge.com/ai-artificial-intelligence/993263/where-does-openai-get-mathematics-trai...
34•kevcampb•29m ago•31 comments

Shopify acquires Tailwind

https://tailwindcss.com/blog/tailwind-is-joining-shopify
1055•EdwinHoksberg•22h ago•405 comments

Larger Pacific Striped Octopus

https://en.wikipedia.org/wiki/Larger_Pacific_striped_octopus
79•olalonde•1d ago•41 comments

What do Visa and Mastercard do? An intro to card networks

https://tautology.town/2026/06/01/card-networks.html
560•evakhoury•1d ago•338 comments

Object storage is all you need

https://www.tigrisdata.com/blog/object-storage-all-need/
9•jpsaccount•1d ago•4 comments

Show HN: Art – draw one stroke, let symmetry complete it

https://mrdee.in/mandala/
19•cyb0rg0•4d ago•7 comments

Growing proof that autonomous cars save lives

https://spectrum.ieee.org/are-self-driving-cars-safe
363•bookofjoe•18h ago•635 comments

Thanks to Siri Recaps, your Apple Watch is always listening

https://www.techradar.com/health-fitness/smartwatches/thanks-to-siri-recaps-your-apple-watch-is-a...
20•geox•1h ago•9 comments

Samsung Debuts zHBM Prototype, Stacking Memory Directly on AI Accelerators

https://www.thelec.net/news/articleView.html?idxno=12835
29•peter_d_sherman•3d ago•4 comments

No Man's Sky Cosmos

https://www.nomanssky.com/cosmos-update/
409•Limb•20h ago•409 comments

Open Source Durable Objects for Postgres

https://solidobjects.dev/blog/introducing-solidobjects
30•cardmagic•1d ago•8 comments

GPT-6 Astra, looped transformers, and hidden reasoning

https://magazine.sebastianraschka.com/p/gpt-6-astra-looped-transformers-and
447•ModelForge•21h ago•144 comments

PlayStation cancels Kojima's PHYSINT, Xbox steps in

https://twitter.com/hideo_kojima_en/status/2097877506401681753
24•HatchedLake721•1h ago•7 comments

AirPods 5

https://www.apple.com/newsroom/2026/09/apple-introduces-airpods-5-with-best-in-class-open-ear-act...
465•awad•18h ago•396 comments

iPhone 18 Pro and iPhone 18 Pro Max

https://www.apple.com/newsroom/2026/09/apple-debuts-iphone-18-pro-and-iphone-18-pro-max/
372•meetpateltech•18h ago•409 comments

Automattic's board forces CEO Matt Mullenweg into leave of absence

https://techcrunch.com/2026/09/09/automattics-board-forces-ceo-matt-mullenweg-into-leave-of-absence/
317•LeoPanthera•11h ago•213 comments

Aardman (Wallace and Gromit) Is Selling Its Original Movie Puppets

https://gizmodo.com/aardman-is-selling-its-original-movie-puppets-this-month-2000807968
59•Gaishan•3d ago•11 comments

All grown-ups were once children, but only few of them remember it

https://mathstodon.xyz/@tao/117244102901892965
198•yurivish•7h ago•150 comments

Show HN: Compute polynomials twice as fast

https://thomasahle.com/fast-polynomials/
97•thomasahle•1d ago•34 comments

Desert Ant Labs: local, fast models that run on device

https://desertant.com/blog/introducing-desert-ant-labs/
459•willwhitedc•1d ago•97 comments

Training a 3.8B LLM to 0.384 CORE for $998

https://hugovergnes.github.io/little-lm-3-8b/
89•Anon84•9h ago•14 comments

I think I hate the internet

https://strategictree.bearblog.dev/i-think-i-hate-the-internet/
89•saikatsg•5h ago•71 comments

Qwen 3.8 follows GPT-5.5 Pro reasoning prefills

https://gist.github.com/wsxiaoys/e0286dc6bb624ff5fdf49e7f4c528ba3
221•wsxiaoys•18h ago•85 comments

Factoring RSA 260

https://cognition.com/blog/factoring-rsa-260
104•samyok•15h ago•12 comments