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.

AOSP Source Releases Are So Late Developers Disassemble Libraries to Report Bugs

https://github.com/android/ndk/issues/2260
129•tharen•1h ago•29 comments

Gemini 4 Argon

https://deepmind.google/models/gemini/
1226•737max•15h ago•860 comments

Sound – Bartosz Ciechanowski (2022)

https://ciechanow.ski/sound/
89•dverlaeckt80•2d ago•13 comments

Felix Rieseberg Homepage

https://felixrieseberg.com/
157•rglover•2h ago•53 comments

Ledger confirms spy chip implant behind wallet crypto thefts

https://www.bleepingcomputer.com/news/security/ledger-confirms-hardware-wallet-spy-implant-behind...
634•thm•1d ago•422 comments

Grace Hopper Celebration Cancelled

https://anitab.org/press-release
143•mooreds•8h ago•52 comments

Show HN: Guitoro, a virtual guitar you strum with your keyboard

https://guitoro.com/
400•talccc•12h ago•106 comments

Redesigning source control at Anthropic

https://www.anthropic.com/engineering/redesigning-source-control
726•Philpax•15h ago•713 comments

900-Page Pre-Pantone Guide to Color from 1692 (With Complete High-Res Scan)

https://www.openculture.com/2026/10/a-900-page-pre-pantone-guide-to-color-from-1692.html
141•mdp2021•1d ago•14 comments

Recreational Programming

https://www.cphiri.dev/presentations/recreational-programming
157•jslakro•9h ago•67 comments

The United Kingdom generates less electricity per person than in the 1970s

https://ourworldindata.org/data-insights/electricity-generation-per-person-in-the-united-kingdom-...
279•karakoram•16h ago•404 comments

Kcgi – minimal CGI and FastCGI library for C/C++

https://kristaps.bsd.lv/kcgi/
16•Bluestein•1d ago•2 comments

I built a tool to track what AI leaders say and how their views change on video

https://blumify.io/people
84•gcsydney•6h ago•53 comments

Arena Allocators in C

https://eliasebner.com/blog/guides/arena-allocators-in-c/
24•dimonomid•2d ago•4 comments

Apple's new 'Switch to Safari' campaign details why you should ditch Chrome

https://9to5mac.com/2026/10/12/apples-new-switch-to-safari-campaign-details-why-you-should-ditch-...
258•ivell•17h ago•424 comments

Show HN: How random are you?

https://lab.specr.net/rigged-draw/
136•vunderba•14h ago•72 comments

Maybe a better way to track the release history of a Git repo

https://blog.plover.com/prog/git/history-proposal.html
36•evakhoury•1d ago•5 comments

MIT class project turns into an FDA-cleared treatment for tremors

https://news.mit.edu/2026/mit-class-project-turns-into-fda-cleared-treatment-for-tremors-1002
196•zdw•1d ago•39 comments

AlphaNIST

https://alpha-nist.github.io/
67•jasonjmcghee•9h ago•23 comments

OpenAI engineer arrested in bugging of executive conference room

https://sfstandard.com/2026/10/13/openai-engineer-arrested-alleged-bugging-executive-conference-r...
543•forthwall•11h ago•458 comments

Do Frameworks Matter Anymore?

https://brookslybrand.com/posts/do-frameworks-matter-anymore/
40•fagnerbrack•6h ago•24 comments

Apple HomeView

https://www.apple.com/apple-homeview/
719•etothet•18h ago•844 comments

Show HN: XY Music Notation

https://music.xy.dev/
68•jmole•13h ago•54 comments

PostEra (YC W20) Is Hiring AI Researchers for Drug Discovery

https://jobs.lever.co/postera/c7d50a36-db0a-4574-bfe4-8700b74a22c0
1•morraa•10h ago

Show HN: Pulse Local, a Mac budgeting app whose AI models run on your machine

https://pulseos.finance
101•niztk•8h ago•55 comments

Hittite royal archive found in Turkey

https://www.thehistoryblog.com/archives/77107
363•benbreen•1d ago•87 comments

11 year old unsoundness bugs found in Rust borrow checker

https://github.com/rust-lang/rust/issues/164250
712•formerly_proven•21h ago•399 comments

AI is a tool and using it well takes skill

https://yyhh.org/blog/2026/10/ai-is-a-tool/
665•layer8•1d ago•531 comments

Hiking the world in search of rare cheeses

https://www.bbc.com/travel/article/20261007-the-man-hiking-the-world-in-search-of-rare-cheeses
88•tosh•1d ago•26 comments

Thunderbird for iOS

https://blog.thunderbird.net/2026/10/thunderbird-for-ios-a-first-look-at-the-native-iphone-email-...
423•mjorgers•16h ago•183 comments