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

Comments

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

The Book of PF, 4th edition

https://nostarch.com/book-of-pf-4th-edition
10•0x54MUR41•1h ago•1 comments

Mobile carriers can get your GPS location

https://an.dywa.ng/carrier-gnss.html
639•cbeuw•15h ago•387 comments

Sometimes Your Job Is to Stay the Hell Out of the Way

https://randsinrepose.com/archives/sometimes-your-job-is-to-stay-the-hell-out-of-the-way/
16•ohjeez•4d ago•2 comments

The history of C# and TypeScript with Anders Hejlsberg | GitHub

https://www.youtube.com/watch?v=uMqx8NNT4xY
46•doppp•4d ago•6 comments

List animals until failure

https://rose.systems/animalist/
137•l1n•7h ago•77 comments

In praise of –dry-run

https://henrikwarne.com/2026/01/31/in-praise-of-dry-run/
152•ingve•12h ago•88 comments

pg_tracing: Distributed Tracing for PostgreSQL

https://github.com/DataDog/pg_tracing
49•tanelpoder•3d ago•7 comments

Drawings of the elements of CMS detector, in the style of Leonardo da Vinci

https://cds.cern.ch/record/1157741/
16•nill0•3d ago•1 comments

Cells use 'bioelectricity' to coordinate and make group decisions

https://www.quantamagazine.org/cells-use-bioelectricity-to-coordinate-and-make-group-decisions-20...
56•marojejian•8h ago•16 comments

Generative AI and Wikipedia editing: What we learned in 2025

https://wikiedu.org/blog/2026/01/29/generative-ai-and-wikipedia-editing-what-we-learned-in-2025/
139•ColinWright•11h ago•57 comments

Opentrees.org (2024)

https://opentrees.org/#pos=1/-37.8/145
68•surprisetalk•4d ago•7 comments

Scientist who helped eradicate smallpox dies at age 89

https://www.scientificamerican.com/article/smallpox-eradication-champion-william-foege-dies-at-89/
210•CrossVR•3d ago•49 comments

Outsourcing thinking

https://erikjohannes.no/posts/20260130-outsourcing-thinking/index.html
137•todsacerdoti•11h ago•122 comments

EV-1 for Lease (1996)

https://www.loe.org/shows/shows.html?programID=96-P13-00047#feature4
24•1970-01-01•2d ago•4 comments

Data Processing Benchmark Featuring Rust, Go, Swift, Zig, Julia etc.

https://github.com/zupat/related_post_gen
94•behnamoh•12h ago•42 comments

Show HN: Moltbook – A social network for moltbots (clawdbots) to hang out

https://www.moltbook.com/
203•schlichtm•3d ago•826 comments

Sparse File LRU Cache

http://ternarysearch.blogspot.com/2026/01/sparse-file-lru-cache.html
25•paladin314159•7h ago•2 comments

Nvidia's 10-year effort to make the Shield TV the most updated Android device

https://arstechnica.com/gadgets/2026/01/inside-nvidias-10-year-effort-to-make-the-shield-tv-the-m...
148•qmr•17h ago•128 comments

Finland looks to introduce Australia-style ban on social media

https://yle.fi/a/74-20207494
602•Teever•15h ago•425 comments

When will CSS Grid Lanes arrive?

https://webkit.org/blog/17758/when-will-css-grid-lanes-arrive-how-long-until-we-can-use-it/
26•feross•9h ago•11 comments

Nintendo DS code editor and scriptable game engine

https://crl.io/ds-game-engine/
127•Antibabelic•14h ago•32 comments

Show HN: Minimal – Open-Source Community driven Hardened Container Images

https://github.com/rtvkiz/minimal
87•ritvikarya98•13h ago•26 comments

Demystifying ARM SME to Optimize General Matrix Multiplications

https://arxiv.org/abs/2512.21473
70•matt_d•12h ago•16 comments

Apple Platform Security (Jan 2026) [pdf]

https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf
165•pieterr•16h ago•119 comments

Nonograms: a practical guide with interactive examples

https://lab174.com/blog/202601-nonograms/
30•merelysounds•4d ago•6 comments

The Saddest Moment (2013) [pdf]

https://www.usenix.org/system/files/login-logout_1305_mickens.pdf
113•tosh•12h ago•23 comments

CPython Internals Explained

https://github.com/zpoint/CPython-Internals
195•yufiz•4d ago•45 comments

Swift is a more convenient Rust (2023)

https://nmn.sh/blog/2023-10-02-swift-is-the-more-convenient-rust
279•behnamoh•10h ago•260 comments

Wikipedia: Sandbox

https://en.wikipedia.org/wiki/Wikipedia:Sandbox
75•zaptrem•1d ago•26 comments

CollectWise (YC F24) Is Hiring

https://www.ycombinator.com/companies/collectwise/jobs/ZunnO6k-ai-agent-engineer
1•OBrien_1107•11h ago