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

Comments

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

I bought Friendster for $30k – Here's what I'm doing with it

https://ca98am79.medium.com/i-bought-friendster-for-30k-heres-what-i-m-doing-with-it-d5e8ddb3991d
242•ca98am79•3h ago•123 comments

Fast16: High-precision software sabotage 5 years before Stuxnet

https://www.sentinelone.com/labs/fast16-mystery-shadowbrokers-reference-reveals-high-precision-so...
111•dd23•3h ago•34 comments

Sawe becomes first athlete to run a sub-two-hour marathon in a competitive race

https://www.bbc.com/sport/athletics/articles/crm1m7e0zwzo
188•berkeleyjunk•3h ago•126 comments

Two Athletes Break Sub-2-HR Marathon in Adizero Adios Pro Evo 3

https://news.adidas.com/running/two-adidas-athletes-sabastian-sawe-and-yomif-kejelcha-break-the-s...
41•canucker2016•2h ago•29 comments

Butterflies are in decline across North America, a look at the Western Monarch

https://www.smithsonianmag.com/science-nature/butterflies-are-in-dramatic-decline-across-north-am...
85•1659447091•2h ago•24 comments

Show HN: AI memory with biological decay (52% recall)

https://github.com/sachitrafa/YourMemory
45•SachitRafa•2h ago•20 comments

AI should elevate your thinking, not replace it

https://www.koshyjohn.com/blog/ai-should-elevate-your-thinking-not-replace-it/
225•koshyjohn•3h ago•181 comments

Magic: The Gathering took me from N2 to Japanese fluency

https://www.tokyodev.com/articles/how-magic-the-gathering-took-me-from-n2-to-japanese-fluency
70•pwim•2d ago•21 comments

Box to Save Memory in Rust

https://dystroy.org/blog/box-to-save-memory/
22•emschwartz•3d ago•3 comments

SWE-bench Verified no longer measures frontier coding capabilities

https://openai.com/index/why-we-no-longer-evaluate-swe-bench-verified/
229•kmdupree•9h ago•135 comments

Running Bare-Metal Rust Alongside ESP-IDF on the ESP32-S3's Second Core

https://tingouw.com/blog/embedded/esp32/run_rust_on_app_core
17•MrBuddyCasino•2d ago•1 comments

MoQ Boy

https://moq.dev/blog/moq-boy/
28•mmcclure•3h ago•3 comments

XOXO Festival Archive

https://xoxofest.com/
33•surprisetalk•2d ago•3 comments

Clay PCB Tutorial

https://feministhackerspaces.cargo.site/Clay-PCB-Tutorial
185•j0r0b0•7h ago•118 comments

The Visible Zorker: Zork 1

https://eblong.com/infocom/visi/zork1/
89•PLenz•7h ago•14 comments

Show HN: Startup Equity Adventure Game

https://options-game-polymathrobotics.pythonanywhere.com/
8•iliabara•1h ago•4 comments

The Last of the Lost Generation

https://porticoquarterly.com/book/the-last-of-the-lost-generation/
5•benbreen•1d ago•1 comments

The 1944 Warsaw Uprising, in Color

https://www.barwypowstania.pl/
106•keiferski•4h ago•43 comments

An AI agent deleted our production database. The agent's confession is below

https://twitter.com/lifeof_jer/status/2048103471019434248
386•jeremyccrane•7h ago•540 comments

Show HN: Free textbook on engineering thermodynamics

https://thermodynamicsbook.com/
93•2DcAf•8h ago•27 comments

Statecharts: hierarchical state machines

https://statecharts.dev/
269•sph•14h ago•76 comments

Show HN: Tiao, A two-player turn-based board game

https://playtiao.com
15•trebeljahr•2h ago•3 comments

GoDaddy gave a domain to a stranger without any documentation

https://anchor.host/godaddy-gave-a-domain-to-a-stranger-without-any-documentation/
509•jamesponddotco•6h ago•202 comments

Chernobyl Wildlife Forty Years On

https://www.bbc.com/future/article/20260424-chernobyl-wildlife-forty-years-on
21•reconnecting•4h ago•0 comments

Orinoco: Young Generation Garbage Collection

https://v8.dev/blog/orinoco-parallel-scavenger
32•plow-tycoon•3d ago•4 comments

Dear friend, you have built a Kubernetes (2024)

https://www.macchaffee.com/blog/2024/you-have-built-a-kubernetes/
65•Wingy•2d ago•104 comments

Dillo Browser Release 3.3.0

https://dillo-browser.org/release/3.3.0/
145•rodarima•6h ago•23 comments

Asahi Linux Progress Linux 7.0

https://asahilinux.org/2026/04/progress-report-7-0/
586•elisaado•13h ago•274 comments

Show HN: Turning a Gaussian Splat into a videogame

https://blog.playcanvas.com/turning-a-gaussian-splat-into-a-videogame/
200•yak32•3d ago•47 comments

Why has there been so little progress on Alzheimer's disease?

https://freakonomics.com/podcast/why-has-there-been-so-little-progress-on-alzheimers-disease/
404•chiefalchemist•23h ago•280 comments