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.

GPT-6 Astra

https://openai.com/index/gpt-6-astra/
1430•kibae•8h ago•1185 comments

.name Termination

https://neil.fraser.name/news/2026/09/03/
1457•pavel_lishin•12h ago•397 comments

Project Xanadu: Even More Hindsight

https://gwern.net/xanadu
21•andsoitis•1h ago•2 comments

Qwen 3.8 27B available on Cerebras at 1500 tokens/s

https://inference-docs.cerebras.ai/models/overview
473•altertable•9h ago•142 comments

New type of dice guarantees no tie when deciding who goes first

https://www.cbc.ca/lite/story/9.7328614
62•colinprince•2d ago•41 comments

The largest electric aircraft just flew [video]

https://www.youtube.com/watch?v=nM86DBOqgPM
218•feb•2d ago•151 comments

How an MIT research project became a global programming language

https://news.mit.edu/2026/how-mit-research-project-became-global-programming-language-0831
25•theanonymousone•3d ago•2 comments

From Hookswitch to Grave

https://computer.rip/2026-06-14-hookswitch-to-grave.html
6•Sniffnoy•52m ago•0 comments

Artificial beaver dams saw juvenile coho salmon survival rates go from 8% to 60%

https://www.discoverwildlife.com/animal-facts/artificial-beaver-dams-california
181•speckx•11h ago•60 comments

Which tools do Claude, Codex and Cursor choose? We measured 17k runs to find out

https://armature.tech/blog/which-tools-coding-agents-install
122•screm•6h ago•45 comments

Porting my 1993 Amiga game to Godot, with an LLM reading the 68000 assembly

https://babyloniantwins.com/blog/porting-a-1993-amiga-game-to-godot/
218•rabahs•13h ago•66 comments

K2 Horizon: A connected fleet of six open models

https://ifm.ai/blog/k2/
269•karimf•11h ago•86 comments

GPS glitched across the US by as much as 33 feet

https://www.sciencealert.com/gps-glitched-across-the-us-by-as-much-as-33-feet-scientists-have-nev...
137•thread_id•1d ago•78 comments

Any Human Ever – One life, drawn at random from all who have ever lived

https://anyhumanever.com/
490•thinkingemote•12h ago•247 comments

Xanadu was waiting for agents

https://zed.dev/blog/agentic-xanadu
96•nsm•2d ago•37 comments

Go grandmaster Shin defeats AI KataGo with a two-stone handicap

https://www.kedglobal.com/artificial-intelligence/newsView/ked202607210007
213•gmays•1d ago•63 comments

GLP-1s are being linked to fewer serious infections, including TB

https://gizmodo.com/ozempic-and-other-glp-1s-are-being-linked-to-fewer-serious-infections-includi...
73•gumby•4h ago•41 comments

Virtual Threads for a scripting language in Java 8 without Loom

https://jactl.io/blog/2026/08/28/jactl-virtual-threads
11•jaccomo•2d ago•1 comments

Tasklet (YC P26) Is Hiring a Customer Success Engineer

https://tasklet.ai/careers/customer-success-engineer
1•mayop100•6h ago

How concerned should we be about Astra's recurrent architecture?

https://www.lesswrong.com/posts/PLisnSFir8y5AHkmP/how-concerned-should-we-be-about-astra-s-recurrent
102•yurivish•10h ago•59 comments

Unusual Suspects

https://neal.fun/unusual-suspects/
122•beeperboy95•1d ago•24 comments

The true horror of Edgar Allan Poe’s stories lies in their confessions

https://yalereview.org/article/emily-ogden-edgar-allan-poe
60•lermontov•1d ago•23 comments

Google Antigravity TOS: 3rd party usage can get Google account suspended

https://twitter.com/GergelyOrosz/status/2095453567955968398
290•tosh•16h ago•200 comments

Ask HN: Who is using MCP in production?

25•sukit•16h ago•38 comments

A PCB business card with a batteryless LED, powered by the phone that taps it

https://www.kevin.md/the-business-card-that-lights-up.md/
49•thekevintang•2d ago•14 comments

OpenAI's GPT-6 Astra on ARC-AGI-3

https://arcprize.org/blog/astra
176•vignesh_warar•7h ago•109 comments

The asteroid currently hitting front end web development

https://nolanlawson.com/2026/08/23/the-asteroid-currently-hitting-frontend-web-development/
93•codechicago277•8h ago•109 comments

How to get a free .arpa domain

https://hawksley.dev/blog/get-free-arpa-domain
126•ethanhawksley•3d ago•13 comments

Ask HN: Why were OpenAI, Claude, and Grok simultaneously down?

343•halcdev•12h ago•530 comments

The browser's main thread is expensive

https://kciter.so/posts/the-expensive-main-thread/en/
380•kciter•2d ago•134 comments