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•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.

Claude for Small Business

https://www.anthropic.com/news/claude-for-small-business
343•neilfrndes•8h ago•288 comments

Show HN: Running the second public ODoH relay

https://numa.rs/blog/posts/odoh-anonymous-dns-without-an-account.html
42•rdme•2h ago•16 comments

Myths about /dev/urandom (2014)

https://www.2uo.de/myths-about-urandom/
14•signa11•1h ago•10 comments

The Tree House: A voyage to the source of a backyard dream

https://www.laphamsquarterly.org/roundtable/tree-house
13•Caiero•2d ago•0 comments

Scorched Earth 2000 – Web

http://www.scorch2000.com/web/
299•meshko•12h ago•110 comments

Linux gaming is faster because Windows APIs are becoming Linux kernel features

https://www.xda-developers.com/linux-gaming-is-getting-faster-because-windows-apis-are-becoming-l...
810•haunter•3d ago•514 comments

What happens when you post a real Monet and say it's AI?

https://twitter.com/jediwolf/status/2054776716770320631
49•nailer•30m ago•16 comments

Leaving the Physical World

https://www.eff.org/pages/leaving-physical-world
49•andsoitis•3d ago•17 comments

Classic 7 is a Windows 10 LTSC mod to look 1:1 to Windows 7

https://classic7.lol/
105•jandeboevrie•5h ago•90 comments

New York, California pension leaders oppose 'extreme' SpaceX control structure

https://www.reuters.com/legal/government/new-york-california-pension-leaders-oppose-extreme-space...
10•2OEH8eoCRo0•32m ago•1 comments

Setting up a free *.city.state.us locality domain (2025)

https://fredchan.org/blog/locality-domains-guide/
578•speckx•22h ago•180 comments

MacBook Neo Deep Dive: Benchmarks, Wafer Economics, and the 8GB Gamble

https://www.jdhodges.com/blog/macbook-neo-benchmarks-analysis/
256•tosh•18h ago•300 comments

Technical Dimensions of Live Feedback in Programming Systems

https://joshuahhh.com/dims-of-feedback/
26•tobr•3d ago•4 comments

A History of IDEs at Google

https://laurent.le-brun.eu/blog/a-history-of-ides-at-google
400•laurentlb•5d ago•263 comments

Saying Goodbye to one line of APL

https://homewithinnowhere.com/posts/2026-05-10-one-line.html#fnref1
14•tosh•3d ago•7 comments

The Emacsification of Software

https://sockpuppet.org/blog/2026/05/12/emacsification/
332•rdslw•1d ago•210 comments

A Claude Code and Codex Skill for Deliberate Skill Development

https://github.com/DrCatHicks/learning-opportunities
86•cdrnsf•9h ago•18 comments

The Siri for Families Apple Will Never Build

https://taoofmac.com/space/blog/2026/05/14/1220
11•rcarmo•1h ago•1 comments

Chess puzzle I found in my dad's old book

https://ardoedo.it/kempelen/
182•Eswo•2d ago•56 comments

Show HN: Nibble

https://github.com/glouw/nibble
68•glouwbug•11h ago•14 comments

Extraordinary Ordinals

https://text.marvinborner.de/2026-04-09-17.html
31•marvinborner•2d ago•12 comments

Avoiding and reducing microplastic false positives from dry glove contact

https://pubs.rsc.org/en/content/articlelanding/2026/ay/d5ay01801c
67•efavdb•11h ago•16 comments

Show HN: Needle: We Distilled Gemini Tool Calling into a 26M Model

https://github.com/cactus-compute/needle
691•HenryNdubuaku•1d ago•197 comments

Cisco workforce reductions

https://blogs.cisco.com/news/our-path-forward
230•ahmedomran8•11h ago•239 comments

The US is winning the AI race where it matters most: commercialization

https://avkcode.github.io/blog/us-winning-ai-race.html
214•akrylov•23h ago•591 comments

The Deathbed Notes of Henry James (1968)

https://www.theatlantic.com/past/docs/unbound/flashbks/james/jnote.htm
15•Hooke•1d ago•0 comments

Xs of Y – roguelike that names itself every run. Written in 4kLoC

https://github.com/nooga/xsofy
196•andsoitis•4d ago•84 comments

Heritability of human life span is ~50% when heritability is redefined

https://dynomight.net/lifespan/
110•surprisetalk•1d ago•64 comments

How can Apple deal with the memory shortage?

https://asymco.com/2026/05/11/the-great-memory-panic-of-2026/
111•tambourine_man•2d ago•127 comments

Reverting the incremental GC in Python 3.14 and 3.15

https://discuss.python.org/t/reverting-the-incremental-gc-in-python-3-14-and-3-15/107014
246•curiousgal•4d ago•101 comments