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.

ClickHouse Acquires Langfuse

https://langfuse.com/blog/joining-clickhouse
13•tin7in•34m ago•2 comments

East Germany balloon escape

https://en.wikipedia.org/wiki/East_Germany_balloon_escape
512•robertvc•16h ago•175 comments

Cloudflare acquires Astro

https://astro.build/blog/joining-cloudflare/
817•todotask2•19h ago•359 comments

High-Level Is the Goal

https://bvisness.me/high-level/
135•tobr•1d ago•59 comments

Cursor's latest “browser experiment” implied success without evidence

https://embedding-shapes.github.io/cursor-implied-success-without-evidence/
551•embedding-shape•19h ago•229 comments

FLUX.2 [Klein]: Towards Interactive Visual Intelligence

https://bfl.ai/blog/flux2-klein-towards-interactive-visual-intelligence
132•GaggiX•10h ago•40 comments

6-Day and IP Address Certificates Are Generally Available

https://letsencrypt.org/2026/01/15/6day-and-ip-general-availability
410•jaas•18h ago•230 comments

Beebo, a wave simulator written in C

https://git.sr.ht/~willowf/beebo/
48•anon25783•3d ago•3 comments

Office app has changed to copilot and now I can't open files

https://old.reddit.com/r/Office365/comments/1q2b28q/office_app_has_changed_to_copilot_and_now_i_cant
21•csmantle•1h ago•1 comments

Drone Hacking Part 1: Dumping Firmware and Bruteforcing ECC

https://neodyme.io/en/blog/drone_hacking_part_1/
62•tripdout•7h ago•6 comments

Every data centre is a U.S. military base

https://www.policyalternatives.ca/news-research/every-data-centre-is-a-u-s-military-base/
44•HotGarbage•2h ago•11 comments

LLM Structured Outputs Handbook

https://nanonets.com/cookbooks/structured-llm-outputs
243•vitaelabitur•1d ago•40 comments

Releasing rainbow tables to accelerate Net-NTLMv1 protocol deprecation

https://cloud.google.com/blog/topics/threat-intelligence/net-ntlmv1-deprecation-rainbow-tables
116•linolevan•12h ago•68 comments

Lies, Damned Lies and Proofs: Formal Methods Are Not Slopless

https://www.lesswrong.com/posts/rhAPh3YzhPoBNpgHg/lies-damned-lies-and-proofs-formal-methods-are-...
11•OgsyedIE•2d ago•1 comments

Dell UltraSharp 52 Thunderbolt Hub Monitor

https://www.dell.com/en-us/shop/dell-ultrasharp-52-thunderbolt-hub-monitor-u5226kw/apd/210-bthw/m...
222•cebert•16h ago•285 comments

Experts Warn of Growing Parrot Crisis in Canada

https://www.ctvnews.ca/ottawa/video/2026/01/06/experts-warn-of-growing-parrot-crisis-in-canada/
59•debo_•4d ago•27 comments

STFU

https://github.com/Pankajtanwarbanna/stfu
822•tanelpoder•16h ago•515 comments

Which is "Bouba", and which is "Kiki"? [video]

https://www.youtube.com/watch?v=1TDIAObsqcs
17•basilikum•6d ago•14 comments

Keifu – A TUI for navigating commit graphs with color and clarity

https://github.com/trasta298/keifu
43•indigodaddy•9h ago•6 comments

Reading across books with Claude Code

https://pieterma.es/syntopic-reading-claude/
101•gmays•15h ago•23 comments

Meditation and Unconscious: A Buddhist Monk and a Neuroscientist (2022)

https://thereader.mitpress.mit.edu/meditation-and-the-unconscious-buddhism-neuroscience-conversat...
11•arunc•4h ago•4 comments

Install.md: A standard for LLM-executable installation

https://www.mintlify.com/blog/install-md-standard-for-llm-executable-installation
72•npmipg•11h ago•90 comments

The 'untouchable hacker god' behind Finland's biggest ever crime

https://www.theguardian.com/technology/2026/jan/17/vastaamo-hack-finland-therapy-notes
28•c420•2h ago•15 comments

Patching the Wii News Channel to serve local news (2025)

https://raulnegron.me/2025/wii-news-pr/
90•todsacerdoti•20h ago•21 comments

Show HN: Tusk Drift – Turn production traffic into API tests

https://github.com/Use-Tusk/tusk-drift-cli
27•jy-tan•1d ago•1 comments

The five orders of ignorance (2000)

https://cacm.acm.org/opinion/the-five-orders-of-ignorance/
60•svilen_dobrev•4d ago•18 comments

HTTP RateLimit Headers

https://dotat.at/@/2026-01-13-http-ratelimit.html
66•zdw•2d ago•13 comments

Emoji Use in the Electronic Health Record is Increasing

https://jamanetwork.com/journals/jamanetworkopen/fullarticle/2843883
80•giuliomagnifico•15h ago•74 comments

Elasticsearch was never a database

https://www.paradedb.com/blog/elasticsearch-was-never-a-database
133•jamesgresql•5d ago•95 comments

Michelangelo's first painting, created when he was 12 or 13

https://www.openculture.com/2026/01/discover-michelangelos-first-painting.html
349•bookofjoe•20h ago•165 comments