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

Comments

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

An AI Agent Published a Hit Piece on Me

https://theshamblog.com/an-ai-agent-published-a-hit-piece-on-me/
154•scottshambaugh•32m ago•66 comments

Email is tough: Major European Payment Processor's Emails rejected by GWorkspace

https://atha.io/blog/2026-02-12-viva
203•thatha7777•2h ago•135 comments

Improving 15 LLMs at Coding in One Afternoon. Only the Harness Changed

http://blog.can.ac/2026/02/12/the-harness-problem/
254•kachapopopow•3h ago•119 comments

A brief history of barbed wire fence telephone networks (2024)

https://loriemerson.net/2024/08/31/a-brief-history-of-barbed-wire-fence-telephone-networks/
37•keepamovin•1h ago•14 comments

The "Crown of Nobles" Noble Gas Tube Display (2024)

https://theshamblog.com/the-crown-of-nobles-noble-gas-tube-display/
97•Ivoah•4h ago•16 comments

Culture Is the Mass-Synchronization of Framings

https://aethermug.com/posts/culture-is-the-mass-synchronization-of-framings
41•mrcgnc•2h ago•18 comments

Apache Arrow is 10 years old

https://arrow.apache.org/blog/2026/02/12/arrow-anniversary/
70•tosh•3h ago•13 comments

The Future for Tyr, a Rust GPU Driver for Arm Mali Hardware

https://lwn.net/Articles/1055590/
51•todsacerdoti•2h ago•14 comments

Warcraft III Peon Voice Notifications for Claude Code

https://github.com/tonyyont/peon-ping
765•doppp•11h ago•242 comments

I Wrote a Scheme in 2025

https://maplant.com/2026-02-09-I-Wrote-a-Scheme-in-2025.html
61•maplant•2d ago•4 comments

Apple patches decade-old iOS zero-day, possibly exploited by commercial spyware

https://www.theregister.com/2026/02/12/apple_ios_263/
124•beardyw•2h ago•67 comments

Discord/Twitch/Snapchat age verification bypass

https://age-verifier.kibty.town/
892•JustSkyfall•17h ago•412 comments

Lines of Code Are Back (and It's Worse Than Before)

https://www.thepragmaticcto.com/p/lines-of-code-are-back-and-its-worse
34•birdculture•1h ago•5 comments

TikTok is tracking you, even if you don't use the app

https://www.bbc.com/future/article/20260210-tiktok-is-tracking-you-even-if-you-dont-use-the-app-h...
72•belter•2h ago•46 comments

So many trees planted in Taklamakan Desert that it's turned into a carbon sink

https://www.livescience.com/planet-earth/plants/china-has-planted-so-many-trees-around-the-taklam...
14•Brajeshwar•22m ago•1 comments

AI agent opens a PR write a blogpost to shames the maintainer who closes it

https://github.com/matplotlib/matplotlib/pull/31132
686•wrxd•5h ago•540 comments

Carl Sagan's Baloney Detection Kit: Tools for Thinking Critically (2025)

https://www.openculture.com/2025/09/the-carl-sagan-baloney-detection-kit.html
90•nobody9999•10h ago•54 comments

Run Pebble OS in Browser via WASM

https://ericmigi.github.io/pebble-qemu-wasm/
32•goranmoomin•3h ago•7 comments

The missing digit of Stela C

https://johncarlosbaez.wordpress.com/2026/02/12/stela-c/
80•chmaynard•8h ago•13 comments

“Nothing” is the secret to structuring your work

https://www.vangemert.dev/blog/nothing
393•spmvg•4d ago•154 comments

Show HN: Inamate – Open-source 2D animation tool (alternative to Adobe Animate)

8•hactually•2d ago•2 comments

US businesses and consumers pay 90% of tariff costs, New York Fed says

https://www.ft.com/content/c4f886a1-1633-418c-b6b5-16f700f8bb0d
22•mraniki•1h ago•12 comments

HeyWhatsThat

https://www.heywhatsthat.com/faq.html
106•1970-01-01•3d ago•21 comments

Using an engineering notebook

https://ntietz.com/blog/using-an-engineering-notebook/
272•evakhoury•2d ago•110 comments

Byte magazine artist Robert Tinney, who illustrated the birth of PCs, dies at 78

https://arstechnica.com/gadgets/2026/02/byte-magazine-artist-robert-tinney-who-illustrated-the-bi...
101•rbanffy•5h ago•18 comments

How to make a living as an artist

https://essays.fnnch.com/make-a-living
182•gwintrob•12h ago•97 comments

Text classification with Python 3.14's ZSTD module

https://maxhalford.github.io/blog/text-classification-zstd/
244•alexmolas•3d ago•54 comments

Hologram v0.7.0: Milestone release for Elixir-to-JavaScript porting initiative

https://hologram.page/blog/porting-initiative-delivers-hologram-v0-7-0
90•bartblast•17h ago•23 comments

NetNewsWire Turns 23

https://netnewswire.blog/2026/02/11/netnewswire-turns.html
323•robin_reala•22h ago•89 comments

Reports of Telnet's death have been greatly exaggerated

https://www.terracenetworks.com/blog/2026-02-11-telnet-routing
146•ericpauley•20h ago•59 comments