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

Comments

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

Returning to Rails in 2026

https://www.markround.com/blog/2026/03/05/returning-to-rails-in-2026/
42•stanislavb•1h ago•23 comments

Show HN: s@: decentralized social networking over static sites

http://satproto.org/
223•remywang•7h ago•83 comments

SBCL: A Sanely-Bootstrappable Common Lisp (2008) [pdf]

https://research.gold.ac.uk/id/eprint/2336/1/sbcl.pdf
8•pabs3•51m ago•0 comments

Temporal: The 9-year journey to fix time in JavaScript

https://bloomberg.github.io/js-blog/post/temporal/
643•robpalmer•16h ago•204 comments

Making WebAssembly a first-class language on the Web

https://hacks.mozilla.org/2026/02/making-webassembly-a-first-class-language-on-the-web/
523•mikece•1d ago•173 comments

WebPKI and You

https://blog.brycekerley.net/2026/03/08/webpki-and-you.html
32•aragilar•2d ago•1 comments

Tested: How Many Times Can a DVD±RW Be Rewritten? Methodology and Results

https://goughlui.com/2026/03/07/tested-how-many-times-can-a-dvd%C2%B1rw-be-rewritten-part-2-metho...
126•giuliomagnifico•3d ago•23 comments

Datahäxan

https://0dd.company/galleries/witches/7.html
34•akkartik•2d ago•3 comments

Many SWE-bench-Passing PRs would not be merged

https://metr.org/notes/2026-03-10-many-swe-bench-passing-prs-would-not-be-merged-into-main/
216•mustaphah•10h ago•96 comments

I was interviewed by an AI bot for a job

https://www.theverge.com/featured-video/892850/i-was-interviewed-by-an-ai-bot-for-a-job
264•speckx•13h ago•245 comments

Don't post generated/AI-edited comments. HN is for conversation between humans

https://news.ycombinator.com/newsguidelines.html#generated
3359•usefulposter•12h ago•1259 comments

Iran-backed hackers claim wiper attack on medtech firm Stryker

https://krebsonsecurity.com/2026/03/iran-backed-hackers-claim-wiper-attack-on-medtech-firm-stryker/
127•2bluesc•4h ago•51 comments

Show HN: A context-aware permission guard for Claude Code

https://github.com/manuelschipper/nah/
92•schipperai•8h ago•38 comments

The MacBook Neo

https://daringfireball.net/2026/03/the_macbook_neo
502•etothet•20h ago•825 comments

Google closes deal to acquire Wiz

https://www.wiz.io/blog/google-closes-deal-to-acquire-wiz
278•aldarisbm•16h ago•167 comments

Entities enabling scientific fraud at scale (2025)

https://doi.org/10.1073/pnas.2420092122
286•peyton•18h ago•194 comments

About memory pressure, lock contention, and Data-oriented Design

https://mnt.io/articles/about-memory-pressure-lock-contention-and-data-oriented-design/
44•vinhnx•3d ago•1 comments

Show HN: I built a tool that watches webpages and exposes changes as RSS

https://sitespy.app
222•vkuprin•15h ago•50 comments

NASA's DART spacecraft changed an asteroid's orbit around the sun

https://www.sciencenews.org/article/spacecraft-changed-asteroid-orbit-nasa
8•pseudolus•3d ago•0 comments

BitNet: 100B Param 1-Bit model for local CPUs

https://github.com/microsoft/BitNet
332•redm•19h ago•160 comments

Faster asin() was hiding in plain sight

https://16bpp.net/blog/post/faster-asin-was-hiding-in-plain-sight/
197•def-pri-pub•17h ago•105 comments

What Happens After You Die? (2016)

https://lamag.com/news/the-end/
30•NaOH•3d ago•16 comments

Challenging the Single-Responsibility Principle

https://kiss-and-solid.com/blog/keep-it-simple
24•WolfOliver•3d ago•11 comments

5,200 holes carved into a Peruvian mountain left by an ancient economy

https://newatlas.com/environment/5-200-holes-peruvian-mountain/
125•defrost•2d ago•61 comments

Show HN: Klaus – OpenClaw on a VM, batteries included

https://klausai.com/
139•robthompson2018•15h ago•78 comments

Meticulous (YC S21) is hiring to redefine software dev

https://jobs.ashbyhq.com/meticulous/3197ae3d-bb26-4750-9ed7-b830f640515e
1•Gabriel_h•10h ago

Against vibes: When is a generative model useful

https://www.williamjbowman.com/blog/2026/03/05/against-vibes-when-is-a-generative-model-useful/
79•takira•1d ago•14 comments

Urea prices

https://tradingeconomics.com/commodity/urea
82•burnt-resistor•5h ago•64 comments

Show HN: Autoresearch@home

https://www.ensue-network.ai/autoresearch
62•austinbaggio•8h ago•10 comments

CNN Explainer – Learn Convolutional Neural Network in Your Browser (2020)

https://poloclub.github.io/cnn-explainer/
50•vismit2000•3d ago•2 comments