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
81•neilfrndes•1h ago•33 comments

Scorched Earth 2000 – Web

http://www.scorch2000.com/web/
183•meshko•4h ago•70 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...
609•haunter•3d ago•411 comments

Cisco workforce reductions

https://blogs.cisco.com/news/our-path-forward
109•ahmedomran8•3h ago•75 comments

Arena AI Model ELO History

https://mayerwin.github.io/AI-Arena-History/
13•mayerwin•1h ago•1 comments

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

https://fredchan.org/blog/locality-domains-guide/
536•speckx•14h ago•169 comments

A History of IDEs at Google

https://laurent.le-brun.eu/blog/a-history-of-ides-at-google
326•laurentlb•4d ago•224 comments

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

https://www.jdhodges.com/blog/macbook-neo-benchmarks-analysis/
156•tosh•10h ago•160 comments

Show HN: Nibble

https://github.com/glouw/nibble
23•glouwbug•3h ago•1 comments

The Emacsification of Software

https://sockpuppet.org/blog/2026/05/12/emacsification/
243•rdslw•22h ago•164 comments

Chess puzzle I found in my dad's old book

https://ardoedo.it/kempelen/
135•Eswo•2d ago•37 comments

Twin brothers wipe 96 government databases minutes after being fired

https://arstechnica.com/tech-policy/2026/05/drop-database-what-not-to-do-after-losing-an-it-job/
368•jnord•1d ago•268 comments

Microsoft BitLocker – YellowKey zero-day exploit

https://www.tomshardware.com/tech-industry/cyber-security/microsoft-bitlocker-protected-drives-ca...
61•cookiengineer•2h ago•26 comments

Princeton mandates proctoring for in-person exams, upending 133 year precedent

https://www.dailyprincetonian.com/article/2026/05/princeton-news-adpol-proctoring-in-person-exami...
294•bookofjoe•9h ago•418 comments

Golden Testing a CAD Library

https://doscienceto.it/blog/posts/2026-04-27-golden-testing-cad.html
5•PaulHoule•2d ago•0 comments

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

https://github.com/nooga/xsofy
170•andsoitis•4d ago•75 comments

Launch HN: Ardent (YC P26) – Postgres sandboxes in seconds with zero migration

https://www.tryardent.com/
80•vc289•12h ago•33 comments

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

https://avkcode.github.io/blog/us-winning-ai-race.html
180•akrylov•15h ago•500 comments

Avoiding and reducing microplastic false positives from dry glove contact

https://pubs.rsc.org/en/content/articlelanding/2026/ay/d5ay01801c
14•efavdb•4h ago•0 comments

AEPs: API Enhancement Proposals

https://github.com/aep-dev/aeps
12•nateb2022•1d ago•4 comments

'A Four-Eyed World' Review: The Story of Spectacles

https://www.wsj.com/arts-culture/books/a-four-eyed-world-review-the-story-of-spectacles-504334ac
3•Hooke•4d ago•0 comments

How can Apple deal with the memory shortage?

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

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

https://dynomight.net/lifespan/
81•surprisetalk•1d ago•51 comments

Marco Polo: Finding a friend with only distance and motion

https://www.jackhogan.me/blog/marco-polo
53•jackhogan11•2d ago•7 comments

delta time

https://www.deltatime.life/
8•mxfh•4h ago•1 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
213•curiousgal•4d ago•83 comments

The other half of AI safety

https://personalaisafety.com/p/the-other-half-of-ai-safety
57•sofiaqt•4h ago•73 comments

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

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

S-100 Virtual Workbench

https://grantmestrength.github.io/S100/
113•rbanffy•13h ago•24 comments

Can a Language Model Paint?

https://www.etive-mor.com/blog/can-a-language-model-paint/
18•liamlaverty•1d ago•7 comments