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.

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...
400•haunter•3d ago•256 comments

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

https://fredchan.org/blog/locality-domains-guide/
446•speckx•8h ago•143 comments

A History of IDEs at Google

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

Medicare's new payment model is built for AI. Most of the tech world has no idea

https://techcrunch.com/2026/05/12/medicares-new-payment-model-is-built-for-ai-and-most-of-the-tec...
24•brandonb•1h ago•15 comments

In-person examinations at Princeton will be proctored starting July 1

https://www.dailyprincetonian.com/article/2026/05/princeton-news-adpol-proctoring-in-person-exami...
182•bookofjoe•3h ago•236 comments

Tell HN: Dont use Claude Design, lost access to my projects after unsubscribing

67•pycassa•1h ago•16 comments

Chess puzzle I found in my dad's old book

https://ardoedo.it/kempelen/
48•Eswo•2d ago•10 comments

Marco Polo: Finding a friend with only distance and motion

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

The Emacsification of Software

https://sockpuppet.org/blog/2026/05/12/emacsification/
147•rdslw•16h ago•103 comments

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

https://github.com/nooga/xsofy
138•andsoitis•3d ago•61 comments

S-100 Virtual Workbench

https://grantmestrength.github.io/S100/
91•rbanffy•7h ago•19 comments

How can Apple deal with the memory shortage?

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

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

https://www.jdhodges.com/blog/macbook-neo-benchmarks-analysis/
126•tosh•4h ago•95 comments

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

https://www.tryardent.com/
57•vc289•6h ago•20 comments

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

https://avkcode.github.io/blog/us-winning-ai-race.html
144•akrylov•9h ago•388 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/
246•jnord•1d ago•172 comments

A sentimental tour of late 1990s and early 2000s hacking tools

https://andreafortuna.org/2026/05/13/amarcord/
37•speckx•4h ago•13 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
184•curiousgal•4d ago•71 comments

New stainless steel can survive conditions for hydrogen production in seawater

https://www.sciencedaily.com/releases/2026/05/260510030950.htm
282•HardwareLust•2d ago•135 comments

"Not Medically Necessary": Helping America's Health Insurers Deny Coverage

https://www.propublica.org/article/evicore-health-insurance-denials-cigna-unitedhealthcare-aetna-...
139•ceejayoz•4h ago•97 comments

Making the news available at no cost is a victory

https://www.sltrib.com/opinion/commentary/2026/05/12/just-days-tribune-reporting/
91•danso•3h ago•101 comments

An idiot's guide to lead optimisation for proteins

https://magnusross.github.io/posts/protein-lead-optimisation-1/
131•magni121•2d ago•11 comments

Leaving GitHub for Forgejo

https://jorijn.com/en/blog/leaving-github-for-forgejo/
508•jorijn•10h ago•269 comments

Meta won't let you block its AI account on Threads

https://www.theverge.com/tech/929091/meta-ai-threads-account-block
61•logickkk1•2h ago•22 comments

Preserving Fisher-Price Pixter

https://dmitry.gr/?r=05.Projects&proj=37.%20Pixter
199•dmitrygr•2d ago•39 comments

Comparing a 1980s memory map to the Raspi Pico

https://medium.com/@noborutakahashi/a-40-year-old-memory-map-comparable-to-todays-raspberry-pi-pi...
7•Schlagbohrer•3d ago•0 comments

Substrate (YC S24) Is Hiring a Technical Success Manager

https://www.ycombinator.com/companies/substrate/jobs/T2fMBhD-technical-success-manager
1•kunle•11h ago

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

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

I moved my digital stack to Europe

https://monokai.com/articles/how-i-moved-my-digital-stack-to-europe/
855•monokai_nl•11h ago•526 comments

Exploring 8 Shaft Weaving

https://algorithmicpattern.org/2026/03/11/exploring-8-shaft-weaving/
16•surprisetalk•2d ago•0 comments