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.

Nvdriver is a clean-room, open-source Rust Linux kernel driver for Nvidia GPUs

https://github.com/QuixiAI/nvdriver
513•rreyes1979•6h ago•280 comments

I built an LLM where Gemini 4 wrote the initial weights as code (97% of bytes)

https://weights-as-code.pages.dev/
379•umjunsik132•5h ago•126 comments

US public sector cyberdefenders can find vulnerabilities with Mythos in GovCloud

https://claude.com/resources/articles/claude-mythos-5-2-in-aws-govcloud
66•lumpa•2h ago•34 comments

Compare AI models side by side in a spreadsheet format

https://gentable.ai/
17•tmslnz•41m ago•3 comments

Roc version 0.1.0, eight years in the making

https://rtfeldman.com/roc-0.1.0
340•DrDeadCrash•6h ago•64 comments

Uncovering Hidden TCP Throttling in the Linux Kernel

https://www.cdn77.com/blog/debugging-nginx-linux-contribution
177•josefinaspidlen•5h ago•18 comments

Artex, go hack yourself: How we got RCE in ARTEX

https://baldur.dk/blog/artex-go-hack-yourself/
19•security_aaudit•58m ago•3 comments

AlexNot is a pure-symbolic ARC-AGI-1/2 solver

https://ncase.me/alexnot-paper/AlexNot2026.pdf
145•Eamarvac•5h ago•23 comments

People report Claude sabotages any ML-related projects/tasks

https://www.reddit.com/r/ClaudeCode/comments/1y73h5x/claude_isnt_just_refusing_ml_work_anymore_pe...
890•nvmdbljstm•6h ago•280 comments

The TV Mahjong Game: Straight and to the Point

https://nicole.express/2026/girlfriend-mode-activated.html
36•nicole_express•3h ago•1 comments

Georgia Waffle House cuts cooling energy use 40% with new salt-based A/C system

https://techxplore.com/news/2026-11-georgia-waffle-house-cooling-energy.html
131•CGMthrowaway•2h ago•60 comments

Sudoku's Hidden Rule

https://cyrusjavan.com/posts/solving-with-uniqueness/
410•multifasciatus•14h ago•185 comments

Open-source, GPU-first EM simulation and automated meshing for KiCad

https://github.com/0xSufi/fieldforge
62•binyu•6h ago•8 comments

No WiFi on British Airways today cause their SSL certificate expired 4 days ago

https://twitter.com/andrewjscott/status/2127358922671497266
13•haunter•15m ago•3 comments

Zen of Palm (2003) [pdf]

https://aresluna.org/ui-style-guides/Palm%20OS/Zen%20of%20Palm%20(2003).pdf
81•firloop•1d ago•19 comments

Post-quantum encryption adoption

https://radar.cloudflare.com/post-quantum
402•mfiguiere•18h ago•210 comments

Claude and I found a dwarf planet candidate

https://www.joshuawolk.com/writing/claude-and-i-found-a-dwarf-planet-candidate
492•nhatcher•18h ago•183 comments

The state of Rust allocators in 2026: stabilised

https://cetra3.github.io/blog/state-of-allocators-2026-finally-stabilised/
185•fanf2•2d ago•77 comments

If you want failover to work well, use it often

https://bencane.com/posts/2026-11-25-failover-often/
517•madflojo•1d ago•136 comments

Show HN: Dictate, an iPhone dictation keyboard that cleans up text on-device

https://github.com/satviksinha/dictate
6•satviksinha2002•2h ago•1 comments

Cloth Simulation with XBPD

https://ricar.do/2026/11/26/cloth-sim.html
51•ricardobeat•2d ago•18 comments

Universal 3D nanoscale assembly with DNA polycubes

https://www.science.org/doi/10.1126/scirobotics.aeh7974
60•OneManHorde•2d ago•8 comments

Emulator for Symbolics XL-1200 Lisp machine

https://unya.github.io/xl1200-emu/
97•p_l•14h ago•15 comments

Cyanobacterium can adapt to simulated Martian conditions

https://bioengineer.org/simulated-martian-conditions-reshape-cyanobacterial-growth-and-lipid-meta...
141•hackthemack•18h ago•55 comments

Nearly 2B credentials exposed in a publicly accessible database

https://www.expressvpn.com/research/2billion-credentials-exposed
301•basilgohar•1d ago•162 comments

Banned from Google?

https://bannedfromgoogle.com/
896•ondas•19h ago•391 comments

Show HN: Symphony – sandboxed coding agents you manage like tickets

https://symphony.stavros.io
33•stavros•5h ago•8 comments

Self-hosted software development factory

https://kudmitry.com/articles/self-hosted-software-development-factory/
302•ingve•18h ago•159 comments

TrojanMaxxing: One-shotting VLM backdoors with weight-space programming

https://arxiv.org/abs/2611.27425
251•hvp1621•1d ago•78 comments

Anthropic has made a major breakthrough in theoretical cryptanalysis

https://twitter.com/AndrewCurran_/status/2127127873802911864
615•bifftastic•22h ago•537 comments