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.

After 7 months, Glasswing overwelms maintainers with underwhelming findings

https://www.vulncheck.com/blog/anthropic-glasswing-november-update
304•panic•5h ago•134 comments

A great old-timey game-programming hack (2013)

https://blog.moertel.com/posts/2013-12-14-great-old-timey-game-programming-hack.html
34•palsecam•1h ago•6 comments

.name Grandfathered

https://neil.fraser.name/news/2026/11/10/
713•p4bl0•10h ago•168 comments

Scion is back and is going to sell a kei truck in America

https://www.theautopian.com/scion-is-back-and-its-going-to-sell-a-freaking-kei-truck-in-america/
358•NaOH•7h ago•264 comments

Smart home devices should work without the internet

https://docodethatmatters.com/home-automation-overnight-success/
754•skittleson•11h ago•269 comments

Six Math Essentials by Terence Tao

https://teorth.github.io/tao-web/six-math-essentials.html
151•digital55•6h ago•27 comments

Starlink Mini 2

https://starlink.com/mini-2
290•robertjfclarke•8h ago•294 comments

Synaptics patent claims stop work on DisplayLink drivers

https://lwn.net/SubscriberLink/1102549/acbc92e00cee3a3f/
320•speckx•9h ago•179 comments

Hacking the Meta Ray-Ban smartglasses

https://p0pcycle.com/2026/11/06/hacking-the-meta-ray-ban-smartglasses/
61•moooooky•3d ago•21 comments

Val Town is winding down

https://blog.val.town/winding-down
320•tosh•9h ago•84 comments

AlphaProof Code

https://deepmind.google/blog/introducing-alphaproof-code/
586•ilreb•15h ago•178 comments

Show HN: Get your coding agent off ~2019 JavaScript, onto modern web primitives

https://github.com/GoogleChrome/modern-web-guidance
281•paulirish•9h ago•87 comments

LLMs Play Zachtronics' TIS-100

https://tis-100.wincon.dev/
16•fairestvalue•2h ago•1 comments

Pinot Noir: inside the criminal gangs making fake wine

https://www.economist.com/interactive/2026/11/06/pinot-noir-inside-the-criminal-gangs-making-fake...
37•axiomdata316•2d ago•9 comments

Hooked on Tuna

https://blue-continuum.com/hooked-on-tuna
5•dnetesn•58m ago•0 comments

What objective is reasoning solving?

https://alexwang.ai/posts/reasoning-as-pathfinding/
113•gmays•7h ago•51 comments

AMD's Zen 6 Family: Feeding the Beast

https://chipsandcheese.com/p/amds-zen-6-family-feeding-the-beast
194•pixelpoet•10h ago•74 comments

Lighting the Skies

https://computer.rip/2026-10-25-searchlights.html
19•dverlaeckt80•3d ago•2 comments

AI is an information gateway. Audiences still don't want it making their news

https://www.niemanlab.org/2026/11/ai-is-becoming-an-information-gateway-audiences-still-dont-want...
16•rdmuser•3h ago•7 comments

$30M Raised to Redo the CPU

https://tendrils.co/blog/2026-11-10/30m-raised
40•shioyama•5h ago•31 comments

Ableton Live 13

https://www.ableton.com/en/live/
289•phaser•14h ago•304 comments

Research taste in history: the importance of learning Babylonian math

https://resobscura.substack.com/p/research-taste-in-history-the-importance
18•benbreen•7h ago•1 comments

A comprehensive study of how fingerprintable users are

https://fingerprinting.study/
80•tomrittervg•8h ago•37 comments

Process Explorer for macOS

https://github.com/microsoft/Procexp-Mac
145•ingve•1d ago•53 comments

Show HN: Valencia, Spain in Sim City/Isometric View

https://tinyvalencia.com/
207•leugim•12h ago•52 comments

AI usage generates a fragile codebase

https://evanxmerz.com/post/ai-usage-generates-a-fragile-codebase/
407•plun9•16h ago•422 comments

Simon Winchester has died

https://www.nytimes.com/2026/11/04/obituaries/simon-winchester-dead.html
78•benbreen•2d ago•28 comments

Sherman Anti-Trust Act (1890)

https://www.archives.gov/milestone-documents/sherman-anti-trust-act
75•hashmap•6h ago•45 comments

What's new in the .NET 11 runtime

https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime
246•stevefan1999•15h ago•178 comments

Show HN: Aircad – Professional drafting and 3D modeling in the browser

https://aircad.com
27•peetle•12h ago•12 comments