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

Comments

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

Hardware Attestation as Monopoly Enabler

https://grapheneos.social/@GrapheneOS/116550899908879585
1392•ChuckMcM•14h ago•459 comments

Local AI needs to be the norm

https://unix.foo/posts/local-ai-needs-to-be-norm/
1014•cylo•14h ago•441 comments

The Greatest Shot in Television: James Burke Had One Chance to Nail This Scene (2024)

https://www.openculture.com/2024/10/the-greatest-shot-in-television.html
129•susam•5h ago•52 comments

Running local models on an M4 with 24GB memory

https://jola.dev/posts/running-local-models-on-m4
275•shintoist•8h ago•83 comments

I'm going back to writing code by hand

https://blog.k10s.dev/im-going-back-to-writing-code-by-hand/
297•dropbox_miner•6h ago•119 comments

7 lines of code, 3 minutes: Implement a programming language (2010)

https://matt.might.net/articles/implementing-a-programming-language/
40•azhenley•3h ago•9 comments

Obsidian plugin was abused to deploy a remote access trojan

https://cyber.netsecops.io/articles/obsidian-plugin-abused-in-campaign-to-deploy-phantom-pulse-rat/
183•cmbailey•9h ago•91 comments

An AI coding agent, used to write code, needs to reduce your maintenance costs

https://www.jamesshore.com/v2/blog/2026/you-need-ai-that-reduces-your-maintenance-costs
137•cratermoon•8h ago•34 comments

Incident Report: CVE-2024-YIKES

https://nesbitt.io/2026/02/03/incident-report-cve-2024-yikes.html
498•miniBill•14h ago•126 comments

Mythos Finds a Curl Vulnerability

https://daniel.haxx.se/blog/2026/05/11/mythos-finds-a-curl-vulnerability/
44•TangerineDream•1h ago•11 comments

Show HN: adamsreview – better multi-agent PR reviews for Claude Code

https://github.com/adamjgmiller/adamsreview
25•adamthegoalie•5h ago•6 comments

Ask HN: What are you working on? (May 2026)

176•david927•14h ago•636 comments

How Fast Does Claude, Acting as a User Space IP Stack, Respond to Pings?

https://dunkels.com/adam/claude-user-space-ip-stack-ping/
66•adunk•8h ago•17 comments

Seeing Birdsong

https://www.lucioarese.net/seeing-birdsong/
18•carabiner•3d ago•1 comments

First tunnel element of the Fehmarnbelt Tunnel immersed

https://www.arup.com/en-us/news/first-fehmarnbelt-tunnel-element-lowered/
87•robin_reala•3d ago•22 comments

dBase: 1979-2026

https://delphinightmares.substack.com/p/dbase-1979-2026
65•deeaceofbase•3d ago•17 comments

Guy Goma's Accidental BBC Interview Lives on After 20 Years

https://www.nytimes.com/2026/05/06/business/media/bbc-guy-goma-interview.html
102•nxobject•2d ago•22 comments

I returned to AWS and was reminded why I left

http://fourlightyears.blogspot.com/2026/05/i-returned-to-aws-and-was-reminded-hard.html
748•andrewstuart•1d ago•524 comments

Traces Of Humanity

https://tracesofhumanity.org/hello-world/
153•alex77456•14h ago•22 comments

The people preserving the scientific practice of bird banding

https://thenarwhal.ca/bird-banding-ontario/
51•bookofjoe•3d ago•0 comments

Stop MitM on the first SSH connection, on any VPS or cloud provider

https://www.joachimschipper.nl/Stop%20MITM%20on%20the%20first%20SSH%20connection,%20on%20any%20VP...
110•JoachimSchipper•2d ago•61 comments

Eight More '8-Bit Era' Microprocessors

https://thechipletter.substack.com/p/eight-more-8-bit-era-microprocessors
69•klelatti•2d ago•22 comments

The locals don't know

https://www.quarter--mile.com/The-Locals-Dont-Know
150•herbertl•15h ago•107 comments

Ice Cream Blending (1965) [pdf]

https://bitsavers.org/pdf/ibm/generalInfo/E20-0156-0_Linear_Programming_-_Ice_Cream_Blending.pdf
6•ok123456•2d ago•1 comments

Shelf Source: Tom MacWright

https://roadlessread.com/views/ss-macwright
13•tobr•1d ago•0 comments

Maryland citizens hit with $2B power grid upgrade for out-of-state AI

https://www.tomshardware.com/tech-industry/artificial-intelligence/maryland-citizens-slapped-with...
230•lemonberry•10h ago•129 comments

Gode Cookery – Authentic Medieval Recipes

http://www.godecookery.com/godeboke/godeboke.htm
12•Mr_Minderbinder•3d ago•1 comments

Idempotency is easy until the second request is different

https://blog.dochia.dev/blog/idempotency/
304•ludovicianul•3d ago•181 comments

Walking slower? Your ears, not your knees, might be the problem

https://www.wsj.com/health/wellness/hearing-loss-walking-speed-iphone-study-c53c482a
114•marc__1•1d ago•71 comments

Task Paralysis and AI

https://g5t.de/articles/20260510-task-paralysis-and-ai/index.html
232•MrGilbert•1d ago•113 comments