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.

Claude Sonnet 5

https://www.anthropic.com/news/claude-sonnet-5
473•marinesebastian•1h ago•245 comments

Claude Code is steganographically marking requests

https://thereallo.dev/blog/claude-code-prompt-steganography
858•kirushik•4h ago•246 comments

Claude Science

https://claude.com/product/claude-science
185•lebovic•2h ago•77 comments

Nano Banana 2 Lite

https://deepmind.google/models/gemini-image/flash-lite/
171•minimaxir•3h ago•54 comments

We Are the Last People Who Know How It Works

https://unix.foo/posts/last-people-who-know-how-it-works/
227•cylo•2h ago•150 comments

I built a mmWave material classification radar

https://gauthier-lechevalier.com/radar
60•GL26•2h ago•18 comments

Looking Ahead to Postgres 19

https://www.snowflake.com/en/blog/engineering/postgresql-19-features-beta/
186•thinkingemote•5h ago•111 comments

County with 37 Data Centers Asks Schools to 'Conserve Electricity'

https://www.404media.co/henrico-virginia-datacenter-energy-cost-email/
274•01-_-•3h ago•134 comments

Don't Make Gates Optional, Make Them Flexible

https://wakamoleguy.com/p/flexible-gates
28•wakamoleguy•3d ago•3 comments

Memoirs of Extraordinary Popular Delusions and the Madness of Crowds (1852)

https://www.gutenberg.org/ebooks/24518
136•lstodd•7h ago•39 comments

Tell HN: Installing Cursor on iOS irreversibly changes your privacy settings

70•zkldi•1h ago•14 comments

Knoppix

https://www.knopper.net/knoppix/index-en.html
171•hoangvmpc•6h ago•79 comments

Show HN: I made a heatmap of 3400 VCs who are open to cold emails

https://apparent.social/heat-map
11•west_subject•1h ago•7 comments

Open Source Low Tech

https://opensourcelowtech.org/
577•grep_it•4d ago•120 comments

Matrix URIs, a URL syntax from Tim Berners-Lee that never shipped (1996)

https://www.w3.org/DesignIssues/MatrixURIs.html
16•napolux•4d ago•5 comments

Building a custom octocopter from scratch with no prior hardware experience

https://karolina.mgdubiel.com/drone/
271•noleary•2d ago•58 comments

Crypto firms have spent $189M so far on 2026 US election, report says

https://www.reuters.com/world/crypto-firms-have-spent-189-million-so-far-2026-us-election-report-...
149•tartoran•3h ago•59 comments

Factorio 2.1 Experimental Release

https://factorio.com/blog/post/fff-444
101•ibobev•3d ago•55 comments

Qwen 3.6 27B is the sweet spot for local development

https://quesma.com/blog/qwen-36-is-awesome/
1117•stared•1d ago•699 comments

European digital ID wallets rely on safety services of Google and Apple

https://waag.org/en/article/european-digital-id-wallets-are-gift-google-and-apple/
646•donohoe•9h ago•279 comments

Zluda 6 release (run unmodified CUDA applications on non-Nvidia GPUs)

https://vosen.github.io/ZLUDA/blog/zluda-update-q1q2-2026/
118•Tiberium•9h ago•11 comments

LongCat-2.0, a large-scale MoE model with 1.6T total and 48B Active

https://longcat.chat/blog/longcat-2.0/
256•benjiro29•19h ago•76 comments

We moved our Bluesky data to Eurosky

https://waag.org/en/article/why-we-moved-our-bluesky-data-eurosky/
126•dotcoma•4h ago•102 comments

Mathematics: Its Content, Methods and Meaning

https://old.maa.org/press/maa-reviews/mathematics-its-content-methods-and-meaning
58•teleforce•3d ago•17 comments

Xsnow "protestware" in Debian

https://lwn.net/SubscriberLink/1079385/3d7a57da58b41aa9/
95•6581•2h ago•72 comments

.self: A new top-level domain designed to support self-hosting

https://hccf.onmy.cloud/2026/06/21/reclaiming-our-digital-selves-hccfs-vision-for-a-human-centere...
647•HumanCCF•1d ago•359 comments

Free the Icons

https://weblog.rogueamoeba.com/2026/06/26/free-the-icons/
645•zdw•3d ago•239 comments

Exercise intensity influences body composition in healthy older adults (2025)

https://www.maturitas.org/article/S0378-5122(25)00571-7/fulltext
180•bookofjoe•9h ago•151 comments

Amazon Seller Reveals Rare Glimpse of Shadow Bribery Market

https://www.bloomberg.com/news/articles/2026-06-24/inside-the-shadow-market-selling-access-to-ama...
34•petethomas•1h ago•15 comments

I'm building a Space Cadet Pinball Machine! [video]

https://www.youtube.com/watch?v=lHQ8c8i42VE
75•skibz•4d ago•15 comments