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

Comments

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

Moltbook

https://www.moltbook.com/
221•teej•3h ago•101 comments

OpenClaw – Moltbot Renamed Again

https://openclaw.ai/blog/introducing-openclaw
31•ed•2h ago•7 comments

Grid: Free, local-first, browser-based 3D printing/CNC/laser slicer

https://grid.space/stem/
263•cyrusradfar•8h ago•89 comments

The Dank Case for Scrolling Window Managers

https://tedium.co/2026/01/29/niri-danklinux-scrolling-window-managers/
57•todsacerdoti•3h ago•23 comments

PlayStation 2 Recompilation Project Is Absolutely Incredible

https://redgamingtech.com/playstation-2-recompilation-project-is-absolutely-incredible/
387•croes•12h ago•177 comments

Project Genie: Experimenting with infinite, interactive worlds

https://blog.google/innovation-and-ai/models-and-research/google-deepmind/project-genie/
554•meetpateltech•14h ago•262 comments

Photoroom (YC S20) Is Hiring a Head of Cross-Platform (Rust) in Paris

https://jobs.ashbyhq.com/photoroom/dc994a7c-e104-46e1-81c3-b88d635398b9
1•ea016•38m ago

Claude Code daily benchmarks for degradation tracking

https://marginlab.ai/trackers/claude-code/
644•qwesr123•17h ago•304 comments

Retiring GPT-4o, GPT-4.1, GPT-4.1 mini, and OpenAI o4-mini in ChatGPT

https://openai.com/index/retiring-gpt-4o-and-older-models/
163•rd•10h ago•235 comments

Stargaze: SpaceX's Space Situational Awareness System

https://starlink.com/updates/stargaze
60•hnburnsy•4h ago•16 comments

Doin' It with a 555: One Chip to Rule Them All

https://aashvik.com/posts/555-revolution/
13•MonkeyClub•2d ago•5 comments

AGENTS.md outperforms skills in our agent evals

https://vercel.com/blog/agents-md-outperforms-skills-in-our-agent-evals
296•maximedupre•18h ago•122 comments

The WiFi only works when it's raining (2024)

https://predr.ag/blog/wifi-only-works-when-its-raining/
162•epicalex•10h ago•54 comments

Backseat Software

https://blog.mikeswanson.com/backseat-software/
73•zdw•9h ago•10 comments

Flameshot

https://github.com/flameshot-org/flameshot
173•OsrsNeedsf2P•12h ago•62 comments

My Mom and Dr. DeepSeek (2025)

https://restofworld.org/2025/ai-chatbot-china-sick/
168•kieto•12h ago•93 comments

The Home Computer Hybrids: Atari, TI, and the FCC

https://technicshistory.com/2026/01/25/the-home-computer-hybrids/
19•cfmcdonald•3d ago•0 comments

Nannou – A creative coding framework for Rust

https://github.com/nannou-org/nannou
16•dmit•2d ago•1 comments

CISA’s acting head uploaded sensitive files into public version of ChatGPT

https://www.politico.com/news/2026/01/27/cisa-madhu-gottumukkala-chatgpt-00749361
129•rurp•2d ago•212 comments

Way AI assistance impacts the formation of coding skills

https://www.anthropic.com/research/AI-assistance-coding-skills
7•vismit2000•1h ago•1 comments

Show HN: Mystral Native – Run JavaScript games natively with WebGPU (no browser)

https://github.com/mystralengine/mystralnative
15•Flux159•2d ago•2 comments

Two days of oatmeal reduce cholesterol level

https://www.uni-bonn.de/en/news/017-2026
99•brandonb•5h ago•66 comments

A lot of population numbers are fake

https://davidoks.blog/p/a-lot-of-population-numbers-are-fake
326•bookofjoe•18h ago•268 comments

Launch HN: AgentMail (YC S25) – An API that gives agents their own email inboxes

141•Haakam21•14h ago•145 comments

Spacecurve: A space-filling curve playground

https://corte.si/posts/spacecurve/announce/
4•cortesi•2d ago•2 comments

Is the RAM shortage killing small VPS hosts?

https://www.fourplex.net/2026/01/29/is-the-ram-shortage-killing-small-vps-hosts/
156•neelc•15h ago•192 comments

The paper model houses of Peter Fritz (2013)

https://socks-studio.com/2013/12/06/the-imaginary-town-of-an-unconscious-architect-the-387-paper-...
11•NaOH•2d ago•1 comments

Show HN: Ourguide – OS wide task guidance system that shows you where to click

https://ourguide.ai
46•eshaangulati•3d ago•22 comments

Employers, please use postmarked letters for job applications (2025)

https://soapstone.mradford.com/employers-use-letters-for-job-applications/
58•MattyRad•8h ago•71 comments

Usenet personality

https://en.wikipedia.org/wiki/Usenet_personality
73•mellosouls•4d ago•53 comments