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.

Statement by Denmark, Finland, France, Germany, the Netherlands,Norway,Sweden,UK

https://www.presidentti.fi/statement-by-denmark-finland-france-germany-the-netherlands-norway-swe...
176•calcifer•39m ago•78 comments

The A in AGI Stands for Ads

https://ossa-ma.github.io/blog/openads
307•calcifer•2h ago•202 comments

More sustainable epoxy thanks to phosphorus

https://www.empa.ch/web/s604/flamm-hemmendes-epoxidharz-nachhaltiger-machen
30•JeanKage•4d ago•7 comments

Command-line Tools can be 235x Faster than your Hadoop Cluster (2014)

https://adamdrake.com/command-line-tools-can-be-235x-faster-than-your-hadoop-cluster.html
151•tosh•7h ago•90 comments

Starting from scratch: Training a 30M Topological Transformer

https://www.tuned.org.uk/posts/013_the_topological_transformer_training_tauformer
78•tuned•5h ago•18 comments

Milk-V Titan: A $329 8-Core 64-bit RISC-V mini-ITX board with PCIe Gen4x16

https://www.cnx-software.com/2026/01/12/milk-v-titan-a-329-octa-core-64-bit-risc-v-mini-itx-mothe...
81•fork-bomber•6d ago•41 comments

A free and open-source rootkit for Linux

https://lwn.net/SubscriberLink/1053099/19c2e8180aeb0438/
73•jwilk•7h ago•14 comments

What is Plan 9?

https://fqa.9front.org/fqa0.html#0.1
94•AlexeyBrin•3h ago•24 comments

ThinkNext Design

https://thinknextdesign.com/home.html
181•__patchbit__•10h ago•78 comments

Show HN: Figma-use – CLI to control Figma for AI agents

https://github.com/dannote/figma-use
41•dannote•11h ago•16 comments

Iconify: Library of Open Source Icons

https://icon-sets.iconify.design/
418•sea-gold•10h ago•45 comments

Erdos 281 solved with ChatGPT 5.2 Pro

https://twitter.com/neelsomani/status/2012695714187325745
251•nl•13h ago•227 comments

ASCII characters are not pixels: a deep dive into ASCII rendering

https://alexharri.com/blog/ascii-rendering
1094•alexharri•1d ago•123 comments

Profession by Isaac Asimov (1957)

https://www.abelard.org/asimov.php
142•bkudria•14h ago•35 comments

Keystone (YC S25) Is Hiring

1•pablo24602•4h ago

jQuery 4

https://blog.jquery.com/2026/01/17/jquery-4-0-0/
521•OuterVale•12h ago•163 comments

Show HN: GibRAM an in-memory ephemeral GraphRAG runtime for retrieval

https://github.com/gibram-io/gibram
48•ktyptorio•10h ago•5 comments

The longest Greek word

https://en.wikipedia.org/wiki/Lopado%C2%ADtemacho%C2%ADselacho%C2%ADgaleo%C2%ADkranio%C2%ADleipsa...
163•firloop•13h ago•73 comments

How London cracked mobile phone coverage on the Underground

https://www.ianvisits.co.uk/articles/how-london-finally-cracked-mobile-phone-coverage-on-the-unde...
126•beardyw•5d ago•137 comments

The grab list: how museums decide what to save in a disaster

https://www.economist.com/1843/2025/11/21/the-grab-list-how-museums-decide-what-to-save-in-a-disa...
46•surprisetalk•4d ago•11 comments

Purdue blocks admission of many Chinese grad students in unwritten policy

https://www.science.org/content/article/purdue-blocks-admission-many-chinese-grad-students-unwrit...
10•bikenaga•1h ago•2 comments

No knives, only cook knives

https://kellykozakandjoshdonald.substack.com/p/no-knives-only-cook-knives
97•firloop•17h ago•57 comments

Consent-O-Matic

https://github.com/cavi-au/Consent-O-Matic
149•throawayonthe•7h ago•78 comments

We put Claude Code in Rollercoaster Tycoon

https://labs.ramp.com/rct
506•iamwil•6d ago•271 comments

Kip: A programming language based on grammatical cases of Turkish

https://github.com/kip-dili/kip
220•nhatcher•20h ago•62 comments

Poking holes into bytecode with peephole optimisations

https://xnacly.me/posts/2026/purple-garden-first-optimisations/
15•ibobev•3d ago•4 comments

Raising money fucked me up

https://blog.yakkomajuri.com/blog/raising-money-fucked-me-up
334•yakkomajuri•22h ago•123 comments

Play chess via Slack DMs or SMS using an ASCII board

https://github.com/dvelton/dm-chess
30•dustfinger•6d ago•9 comments

The Olivetti Company

https://www.abortretry.fail/p/the-olivetti-company
220•rbanffy•1w ago•61 comments

If you put Apple icons in reverse it looks like someone getting good at design

https://mastodon.social/@heliographe_studio/115890819509545391
694•lateforwork•17h ago•263 comments