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

Comments

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

https://github.com/antonmedv/textarea
110•medv•1h ago•43 comments

Fabrice Bellard: Biography [pdf]

https://www.ipaidia.gr/wp-content/uploads/2020/12/117-2020-fabrice-bellard.pdf
109•lioeters•3h ago•32 comments

Spaced Repetition for Efficient Learning

https://gwern.net/spaced-repetition
22•tsenturk•45m ago•1 comments

Show HN: Vibium – Browser automation for AI and humans, by Selenium's creator

https://github.com/VibiumDev/vibium
129•hugs•3h ago•63 comments

When Compilers Surprise You

https://xania.org/202512/24-cunning-clang
174•brewmarche•8h ago•72 comments

Qntm's Power Tower Toy

https://qntm.org/files/knuth/knuth.html
21•ravenical•4d ago•8 comments

Keystone (YC S25) is hiring engineer #1 to automate coding

https://www.ycombinator.com/companies/keystone/jobs/J3t9XeM-founding-engineer
1•pablo24602•33m ago

Nvidia buying AI chip startup Groq for about $20B in cash

https://www.cnbc.com/2025/12/24/nvidia-buying-ai-chip-startup-groq-for-about-20-billion-biggest-d...
51•nickrubin•32m ago•17 comments

A faster path to container images in Bazel

https://www.tweag.io/blog/2025-12-18-rules_img/
43•malt3•6d ago•17 comments

Some Epstein file redactions are being undone

https://www.theguardian.com/us-news/2025/dec/23/epstein-unredacted-files-social-media
904•vinni2•1d ago•704 comments

Beijing is enforcing tough rules to ensure chatbots don’t misbehave

https://www.wsj.com/tech/ai/china-is-worried-ai-threatens-party-ruleand-is-trying-to-tame-it-bfdc...
40•bookofjoe•1h ago•8 comments

I'm returning my Framework 16

https://yorickpeterse.com/articles/im-returning-my-framework-16/
97•YorickPeterse•8h ago•140 comments

X-ray: a Python library for finding bad redactions in PDF documents

https://github.com/freelawproject/x-ray
651•rendx•23h ago•110 comments

Litex: Formal math for everyone – set theory examples with Lean comparison

https://litexlang.com/doc/How_Litex_Works/Litex_vs_Lean_Set_Theory_Examples
7•litexlang•5d ago•1 comments

European Majority favours more social media regulation

https://yougov.co.uk/technology/articles/53241-european-political-monthly-where-do-europeans-stan...
64•snowpid•2h ago•74 comments

Researchers achieved 1,270 Wh/L in an anode-free lithium metal battery

https://postech.ac.kr/eng/research/research_results.do?mode=view&articleNo=43617&title=Anode-Free...
86•giuliomagnifico•3h ago•41 comments

Avoid Mini-Frameworks

https://laike9m.com/blog/avoid-mini-frameworks,171/
96•laike9m•9h ago•73 comments

Looking for Decent Conversation?

62•kmstout•2h ago•6 comments

Making a game on a custom bytecode VM in 7 days and 3kB

https://laurent.le-brun.eu/blog/making-a-game-on-a-custom-bytecode-vm-in-7-days-and-3kb
62•laurentlb•5d ago•9 comments

Fabrication Techniques Using Myco-Materials

https://encyclopedia.pub/entry/27602
11•andsoitis•2d ago•1 comments

Games’ affordance of childlike wonder and reduced burnout risk in young adults

https://games.jmir.org/2025/1/e84219/
121•azalemeth•7h ago•103 comments

The e-scooter isn't new – London was zooming around on Autopeds a century ago

https://www.ianvisits.co.uk/articles/the-e-scooter-isnt-new-london-was-zooming-around-on-autopeds...
120•zeristor•13h ago•94 comments

The Port I couldn't Ship

https://ammil.industries/the-port-i-couldnt-ship/
87•cjlm•6d ago•44 comments

Unifi Travel Router

https://blog.ui.com/article/travel-in-style-unifi-style-unifi-travel-router
434•flurdy•21h ago•374 comments

AMD entered the CPU market with reverse-engineered Intel 8080 clone 50 years ago

https://www.tomshardware.com/pc-components/cpus/amd-first-entered-the-cpu-market-with-reverse-eng...
136•ksec•7h ago•67 comments

LVM Thin Provisioning (2016)

https://storageapis.wordpress.com/2016/06/24/lvm-thin-provisioning/
18•indigodaddy•2d ago•5 comments

My 2026 Open Social Web Predictions

https://www.timothychambers.net/2025/12/23/my-open-social-web-predictions.html
57•todsacerdoti•5h ago•48 comments

Nabokov's guide to foreigners learning Russian

https://twitter.com/haravayin_hogh/status/2003299405907247502
207•flaxxen•20h ago•389 comments

Spice: A 40-year old open-source success story (2011)

https://www.edn.com/spice-a-40-year-old-open-source-success-story/
11•stmw•4h ago•1 comments

Fabrice Bellard Releases MicroQuickJS

https://github.com/bellard/mquickjs/blob/main/README.md
1320•Aissen•1d ago•505 comments