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.

Transcribe.cpp

https://workshop.cjpais.com/projects/transcribe-cpp
439•sebjones•7h ago•79 comments

Searchable field-level encryption on Supabase with CipherStash

https://supabase.com/blog/searchable-field-level-encryption-with-cipherstash
31•dandraper•3d ago•6 comments

Speech Recognition and TTS in less than 500kb

https://github.com/moonshine-ai/moonshine/tree/main/micro
425•petewarden•4d ago•53 comments

Codex Resets

https://codex-resets.com/
159•denysvitali•8h ago•113 comments

Better and Cheaper Than IPTV

https://github.com/stupside/castor
163•xonery•7h ago•39 comments

The Kimi K3 Moment

https://stephen.bochinski.dev/blog/2026/07/18/the-kimi-k3-moment/
357•sbochins•14h ago•389 comments

Scrying the AMD GFX1250 LLVM Tea Leaves

https://chipsandcheese.com/p/scrying-the-amd-gfx1250-llvm-tea
18•mfiguiere•3h ago•0 comments

Mathematicians still don't know the fastest way to multiply numbers

https://www.scientificamerican.com/article/mathematicians-still-dont-know-the-fastest-way-to-mult...
79•beardyw•5d ago•52 comments

GPT-5.6 used a prompt to close a 30-year gap in convex optimization

https://old.reddit.com/r/math/comments/1uxj3cy/after_openais_cdc_proof_announcement_gpt56_used_a/
553•mbustamanter•19h ago•353 comments

Hardcore IndieWeb: Run your own website 100% independently for only $0.01/day

https://www.neatnik.net/hardcore-indieweb
149•cdrnsf•10h ago•90 comments

If You Build It, They Will Come

https://www.benlandautaylor.com/p/if-you-build-it-they-will-come
385•barry-cotter•16h ago•132 comments

Making Software: How to make a font

https://www.makingsoftware.com/chapters/how-to-make-a-font
33•Garbage•4d ago•5 comments

Classic Amiga titles, free to download

https://amigafreeware.downer.tech/
106•doener•10h ago•15 comments

A Visual Catalog of Retro Macintosh Software

https://www.marciot.com/mac68k-visual-catalog/
34•zdw•1w ago•5 comments

Setting up your spare Mac for Claude Code to control, a step-by-step guide

https://ykdojo.github.io/claude-controls-mac/
225•ykev•16h ago•153 comments

I'm Making Strandfall, a Solarpunk Orienteering Larp

https://mssv.net/2026/04/29/im-making-strandfall-a-solarpunk-orienteering-larp/
143•surprisetalk•5d ago•18 comments

Goodbye, and Thanks for All the Bikesheds

https://queue.acm.org/detail.cfm?id=3818307
207•Ygg2•14h ago•208 comments

Mayor Mamdani Says Landlords Can't Use AI Images to Advertise Properties

https://petapixel.com/2026/07/16/mayor-mamdani-says-landlords-cant-secretly-use-ai-images-to-adve...
448•gnabgib•9h ago•198 comments

Elixir-lang.org has a new design

https://elixir-lang.org/
223•bbg2401•16h ago•124 comments

AI Mania Is Eviscerating Global Decision-Making

https://ludic.mataroa.blog/blog/ai-mania-is-eviscerating-global-decision-making/#fnref:3
181•subset•6h ago•66 comments

Gleam Is Now on Tangled

https://tangled.org/gleam.run/gleam
231•nerdypepper•16h ago•144 comments

Using self-hosted Umami for iOS app analytics

https://hjerpbakk.com/blog/2026/07/14/umami-for-apps
5•Sankra•4d ago•1 comments

Fable 5 vs. GPT-5.6 Sol on an NP-Hard Problem: Does /goal help?

https://charlesazam.com/blog/fable-5-gpt-5-6-sol-goal/
235•couAUIA•21h ago•115 comments

Is this the end of the once-mighty GoPro?

https://amateurphotographer.com/latest/photo-news/going-going-gone-is-this-the-end-of-the-once-mi...
215•aanet•4d ago•473 comments

Deepsec

https://github.com/vercel-labs/deepsec
22•handfuloflight•6h ago•0 comments

Developing an Intuitive Sense of Scale

https://magworld.pw
27•vismit2000•4d ago•4 comments

I built a browser-based P2P file transfer tool using WebRTC

https://airdows.com/
17•SamOkampo•5h ago•11 comments

REO Trucks I4 4WD Pickup Truck Starts at $21,500

https://reotrucks.com
86•b_mc2•13h ago•136 comments

Co-evolution of self-replication and function in a digital primordial soup

https://arxiv.org/abs/2607.09211
66•vicgalle_•11h ago•12 comments

What's the deal with all the random weekly quota resets for agents lately?

https://minimaxir.com/2026/07/agent-quota-reset/
50•minimaxir•13h ago•53 comments