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

Comments

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

Qwen3.6-35B-A3B: Agentic Coding Power, Now Open to All

https://qwen.ai/blog?id=qwen3.6-35b-a3b
228•cmitsakis•1h ago•103 comments

€54k spike in 13h from unrestricted Firebase browser key accessing Gemini APIs

https://discuss.ai.google.dev/t/unexpected-54k-billing-spike-in-13-hours-firebase-browser-key-wit...
290•zanbezi•2h ago•197 comments

The Future of Everything Is Lies, I Guess: Where Do We Go from Here?

https://aphyr.com/posts/420-the-future-of-everything-is-lies-i-guess-where-do-we-go-from-here
117•aphyr•1h ago•69 comments

Claude Opus 4.7

https://www.anthropic.com/claude/opus
94•AlphaWeaver•21m ago•41 comments

Cloudflare Email Service: now in public beta. Ready for your agents

https://blog.cloudflare.com/email-for-agents/
55•jilles•1h ago•23 comments

IPv6 traffic crosses the 50% mark

https://www.google.com/intl/en/ipv6/statistics.html?yzh=28197
548•Aaronmacaron•1d ago•350 comments

Cloudflare's AI Platform: an inference layer designed for agents

https://blog.cloudflare.com/ai-platform/
50•nikitoci•1h ago•15 comments

Darkbloom – Private inference on idle Macs

https://darkbloom.dev
376•twapi•10h ago•184 comments

Show HN: MacMind – A transformer neural network in HyperCard on a 1989 Macintosh

https://github.com/SeanFDZ/macmind
10•hammer32•1h ago•0 comments

AI cybersecurity is not proof of work

https://antirez.com/news/163
77•surprisetalk•4h ago•37 comments

FSF trying to contact Google about spammer sending 10k+ mails from Gmail account

https://daedal.io/@thomzane/116410863009847575
263•pabs3•11h ago•168 comments

Modern Microprocessors – A 90-Minute Guide

https://www.lighterra.com/articles/
93•Flex247A•4d ago•11 comments

Ancient DNA reveals pervasive directional selection across West Eurasia [pdf]

https://reich.hms.harvard.edu/sites/reich.hms.harvard.edu/files/inline-files/2026_Akbari_Nature_s...
41•Metacelsus•3h ago•23 comments

Claude Opus 4.7 Model Card

https://anthropic.com/claude-opus-4-7-system-card
20•adocomplete•18m ago•4 comments

Codex Hacked a Samsung TV

https://blog.calif.io/p/codex-hacked-a-samsung-tv
107•campuscodi•4h ago•75 comments

Show HN: Agent-cache – Multi-tier LLM/tool/session caching for Valkey and Redis

4•kaliades•2h ago•0 comments

PHP 8.6 Closure Optimizations

https://wiki.php.net/rfc/closure-optimizations
36•moebrowne•2d ago•5 comments

Mozilla Thunderbolt

https://www.thunderbolt.io/
57•dabinat•1h ago•48 comments

Cybersecurity looks like proof of work now

https://www.dbreunig.com/2026/04/14/cybersecurity-is-proof-of-work-now.html
501•dbreunig•1d ago•182 comments

Fly Drones from the Browser

https://fpvsim.com/sim
7•mosfets•3d ago•7 comments

RedSun: System user access on Win 11/10 and Server with the April 2026 Update

https://github.com/Nightmare-Eclipse/RedSun
131•airhangerf15•10h ago•32 comments

RamAIn (YC W26) Is Hiring

https://www.ycombinator.com/companies/ramain/jobs/bwtwd9W-founding-gtm-operations-lead
1•svee•7h ago

Long Instruction Word architectures and the ELI-512

https://dl.acm.org/doi/10.1145/800046.801649
14•rbanffy•4d ago•1 comments

There's yet another study about how bad AI is for our brains

https://www.engadget.com/ai/theres-yet-another-study-about-how-bad-ai-is-for-our-brains-183418494...
10•speckx•20m ago•3 comments

ChatGPT for Excel

https://chatgpt.com/apps/spreadsheets/
267•armcat•17h ago•167 comments

The paper computer

https://jsomers.net/blog/the-paper-computer
194•jsomers•3d ago•57 comments

North American English Dialects

https://aschmann.net/AmEng/
79•skogstokig•11h ago•44 comments

Moving a large-scale metrics pipeline from StatsD to OpenTelemetry / Prometheus

https://medium.com/airbnb-engineering/building-a-high-volume-metrics-pipeline-with-opentelemetry-...
55•jmarbach•9h ago•13 comments

Apple accelerates eco progress with highest-ever recycled materials

https://www.apple.com/newsroom/2026/04/apple-accelerates-progress-with-highest-ever-recycled-mate...
66•salkahfi•2h ago•57 comments

Cal.com is going closed source

https://cal.com/blog/cal-com-goes-closed-source-why
359•Benjamin_Dobell•23h ago•280 comments