frontpage.
newsnewestaskshowjobs

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•1y ago

Comments

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

Pledging Another $400k to the Zig Software Foundation

https://mitchellh.com/writing/zig-donation-2026
387•tosh•2h ago•109 comments

Never Give Them Your Face

https://nevergivethemyourface.com/
331•audiodude•2h ago•202 comments

Moebius: 0.2B image inpainting model with 10B-level performance

https://hustvl.github.io/Moebius/
55•DSemba•2h ago•9 comments

Claude Code's "extended thinking" is a summary- not authentic thinking

https://patrickmccanna.net/the-text-in-claude-codes-extended-thinking-output-is-not-authentic/
81•0o_MrPatrick_o0•1h ago•65 comments

Deno Desktop

https://docs.deno.com/runtime/desktop/
789•GeneralMaximus•10h ago•309 comments

Chevron signs 20-year power agreement with Microsoft for West Texas data center

https://www.chevron.com/newsroom/2026/q2/chevron-signs-20-year-power-agreement-with-microsoft-for...
31•cdrnsf•2h ago•22 comments

Codex logging bug may write TBs to local SSDs

https://github.com/openai/codex/issues/28224
306•vantareed•8h ago•169 comments

GLM 5.2 vs. Opus

https://techstackups.com/comparisons/glm-5.2-vs-opus/
349•ritzaco•8h ago•244 comments

Show HN: Got sick of ads, so I made my own logic puzzle site

https://puzzlelair.com/
62•HaxleRose•3h ago•38 comments

Die analysis of the 8087 math coprocessor's fast bit shifter

https://www.righto.com/2020/05/die-analysis-of-8087-math-coprocessors.html
23•Jimmc414•2h ago•7 comments

DHL Set to Transport Goods on New Wind-Powered Cargo Ships

https://www.wsj.com/pro/sustainable-business/dhl-set-to-transport-goods-on-new-wind-powered-cargo...
37•julienchastang•1h ago•11 comments

I built Ponytrail, a local audit trail for AI coding-agent edits

https://github.com/0xroylee/ponytrail
16•1997roylee•1h ago•8 comments

Git is forever. I'm building Oak anyways

https://oak.space/blog
8•zdgeier•23m ago•0 comments

Help I accidentally a wigglegram

https://lmao.center/blog/wiggle-accidents/
401•gregsadetsky•2d ago•94 comments

Did my old job only exist because of fraud?

https://david.newgas.net/did-my-old-job-only-exist-because-of-fraud/
741•advisedwang•18h ago•335 comments

Apertus – Open Foundation Model for Sovereign AI

https://apertvs.ai/
494•T-A•18h ago•165 comments

Maria Isabel Sánchez Vegara on Her 100th "Little People, Big Dreams" Book

https://www.amightygirl.com/blog?p=36753
23•zeristor•2d ago•2 comments

Granularity comes at a cost – Game Theory

https://www.sidhantbansal.com/2026/Granularity-comes-at-a-cost/
16•sidhantbansal•2d ago•1 comments

Munich 1991: The Roots of the Current AI Boom

https://people.idsia.ch/~juergen/ai-boom-roots-munich-1991.html
166•tosh•3d ago•71 comments

Why Drawing Tablet Brands Won't Collaborate on Linux Floss Drivers

https://www.davidrevoy.com/article1154/why-drawing-tablet-brands-wont-collaborate-on-linux-floss-...
124•Tomte•3h ago•46 comments

There is minimal downside to switching to open models

https://www.marble.onl/posts/cancel_claude.html
333•amarble•19h ago•279 comments

Nintendo Wii U games running from a 1980's Bernoulli disk [video]

https://www.youtube.com/watch?v=8GZDOpV2OXk
4•zdw•19h ago•0 comments

Investors get real-time view of UK bond market activity for the first time

https://www.fca.org.uk/news/press-releases/investors-get-real-time-view-uk-bond-market-activity-f...
81•monkeydust•8h ago•56 comments

Manticore Search 27.1.5: Auth, sharding, conversational and faster vector search

https://manticoresearch.com/blog/manticore-search-27-1-5/
30•snikolaev•5h ago•1 comments

Alan Greenspan Dies at 100; Led Fed During Boom Before 2008 Bust

https://www.bloomberg.com/news/articles/2026-06-22/alan-greenspan-dies-at-100-led-fed-during-boom...
100•helsinkiandrew•4h ago•68 comments

Nvidia Halos

https://www.nvidia.com/en-us/ai-trust-center/halos/autonomous-vehicles/
47•ilreb•2h ago•26 comments

Sakana Fugu

https://sakana.ai/fugu/
182•Finbarr•13h ago•104 comments

My 1992 view of the problems of computer programming in 1992

https://blog.plover.com/prog/fortran-i.html
76•speckx•3d ago•37 comments

Memory Safe Inline Assembly

https://fil-c.org/inlineasm
153•pizlonator•2d ago•36 comments

Rive, Fast and reliable background jobs in Go

https://github.com/riverqueue/river
14•mountainview•5h ago•2 comments