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•7mo ago

Comments

wahern•7mo 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•7mo 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•7mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•7mo 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•7mo 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•7mo 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•7mo ago
Learning such a language doesn’t mean I can use it.
o11c•7mo 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•7mo 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•7mo 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•7mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•7mo ago
It still is a thing, though.
cperciva•7mo 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•7mo 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.

8M Users' AI Conversations Sold for Profit by "Privacy" Extensions

https://www.koi.ai/blog/urban-vpn-browser-extension-ai-conversations-data-collection
74•takira•50m ago•18 comments

Quill OS is an open-source, fully-functional standalone OS for Kobo's eReaders

https://quill-os.org/
133•Curiositry•3h ago•40 comments

Native vs. emulation: World of Warcraft game performance on Snapdragon X Elite

https://rkblog.dev/posts/pc-hardware/pc-on-arm/x86_versus_arm_native_game/
21•geekman7473•4h ago•5 comments

Ideas aren't getting harder to find

https://asteriskmag.com/issues/12-books/ideas-arent-getting-harder-to-find
85•mitchbob•3h ago•36 comments

Nature's many attempts to evolve a Nostr

https://newsletter.squishy.computer/p/natures-many-attempts-to-evolve-a
133•fiatjaf•5d ago•74 comments

“Are you the one?” is free money

https://blog.owenlacey.dev/posts/are-you-the-one-is-free-money/
201•samwho•3d ago•44 comments

Economics of Orbital vs. Terrestrial Data Centers

https://andrewmccalip.com/space-datacenters
55•flinner•5h ago•98 comments

Essential Semiconductor Physics [pdf]

https://nanohub.org/resources/43623/download/Essential_Semiconductor_Physics.pdf
126•akshatjiwan•2d ago•6 comments

SoundCloud confirms breach after member data stolen, VPN access disrupted

https://www.bleepingcomputer.com/news/security/soundcloud-confirms-breach-after-member-data-stole...
50•technonerd•2h ago•3 comments

Rollstack (YC W23) is hiring multiple software engineers (TypeScript) US/Canada

https://www.ycombinator.com/companies/rollstack-2/jobs/QPqpb1n-software-engineer-typescript-us-ca...
1•yjallouli•2h ago

Umbrel – Personal Cloud

https://umbrel.com
141•oldfuture•8h ago•85 comments

Ford kills the All-Electric F-150

https://www.wired.com/story/ford-kills-electric-f-150-lightning-for-hybrid/
235•sacred-rat•6h ago•363 comments

Understanding carriage

https://seths.blog/2025/12/understanding-carriage/
26•herbertl•5d ago•6 comments

In Defense of Matlab Code

https://runmat.org/blog/in-defense-of-matlab-whiteboard-style-code
88•finbarr1987•3d ago•86 comments

The Bob Dylan concert for just one person

https://www.flaggingdown.com/p/the-bob-dylan-concert-for-just-one
44•NaOH•3h ago•7 comments

A kernel bug froze my machine: Debugging an async-profiler deadlock

https://questdb.com/blog/async-profiler-kernel-bug/
62•bluestreak•7h ago•14 comments

Chafa: Terminal Graphics for the 21st Century

https://hpjansson.org/chafa/
116•birdculture•9h ago•20 comments

Secret Documents Show Pepsi and Walmart Colluded to Raise Food Prices

https://www.thebignewsletter.com/p/secret-documents-show-pepsi-and-walmart
250•connor11528•6h ago•81 comments

Cosmic-ray bath in a past supernova gives birth to Earth-like planets

https://www.science.org/doi/10.1126/sciadv.adx7892
85•toomuchtodo•10h ago•30 comments

Light intensity steers molecular assemblies into 1D, 2D or 3D structures

https://phys.org/news/2025-11-intensity-molecular-1d-2d-3d.html
3•PaulHoule•5d ago•0 comments

Fix HDMI-CEC weirdness with a Raspberry Pi and a $7 cable

https://johnlian.net/posts/hdmi-cec/
205•jlian•6h ago•104 comments

The appropriate amount of effort is zero

https://expandingawareness.org/blog/the-appropriate-amount-of-effort-is-zero/
69•gmays•7h ago•44 comments

John Updike Wrote It All Down

https://newrepublic.com/article/201598/john-updike-wrote
7•samclemens•6d ago•0 comments

Upcoming Changes to Let's Encrypt Certificates

https://community.letsencrypt.org/t/upcoming-changes-to-let-s-encrypt-certificates/243873
253•schmuckonwheels•8h ago•209 comments

JetBlue flight averts mid-air collision with US Air Force jet

https://www.reuters.com/world/americas/jetblue-flight-averts-mid-air-collision-with-us-air-force-...
151•divbzero•5h ago•66 comments

We are discontinuing the dark web report

https://support.google.com/websearch/answer/16767242?hl=en
106•satertek•12h ago•35 comments

I'm Kenyan. I don't write like ChatGPT, ChatGPT writes like me

https://marcusolang.substack.com/p/im-kenyan-i-dont-write-like-chatgpt
585•florian_s•15h ago•383 comments

“Super secure” messaging app leaks everyone's phone number

https://ericdaigle.ca/posts/super-secure-maga-messaging-app-leaks-everyones-phone-number/
534•e_daigle•8h ago•252 comments

Carrier Landing in Top Gun for the NES

https://relaxing.run/blag/posts/top-gun-landing/
358•todsacerdoti•13h ago•150 comments

Problems with D-Bus on the Linux desktop

https://blog.vaxry.net/articles/2025-dbusSucks
283•LorenDB•8h ago•204 comments