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.

Understanding the AI Economy

https://blog.google/innovation-and-ai/technology/research/understanding-the-ai-economy/
34•swolpers•29m ago•23 comments

Terence Tao's ChatGPT conversation about the Jacobian Conjecture counterexample

https://chatgpt.com/share/6a5fdc7a-d6f8-83e8-bbea-8deb42cfed56
948•gmays•19h ago•549 comments

Cruller: Bun's Zig Runtime, Continued on Zig 0.16

https://ziggit.dev/t/cruller-buns-zig-runtime-continued-on-zig-0-16/16734
83•Erenay09•7h ago•47 comments

Quality non-fiction books are the antithesis of AI slop

https://resobscura.substack.com/p/quality-non-fiction-books-are-the
405•benbreen•22h ago•170 comments

Escape IntelliJ: Scala and Kotlin LSPs on Emacs Eglot

https://jointhefreeworld.org/blog/articles/emacs/emacs-eglot-scala-kotlin/index.html
96•jjba23•2d ago•78 comments

The Unity CLI: manage Unity from your terminal

https://unity.com/blog/meet-the-unity-cli
24•nateb2022•1d ago•6 comments

New Framework Desktop Option with AMD Ryzen AI Max+ Pro 495 and 192GB Memory

https://frame.work/desktop?tab=192gb-coming-soon
38•PhilippGille•1h ago•31 comments

EU fines Google €890M for competition breaches over search and apps

https://www.theguardian.com/technology/2026/jul/23/eu-fines-google-for-competition-breaches-over-...
90•Stevvo•2h ago•85 comments

Everyone should know SIMD

https://mitchellh.com/writing/everyone-should-know-simd
491•WadeGrimridge•18h ago•178 comments

Show HN: Bento - An entire PowerPoint in one HTML file (edit+view+data+collab)

https://bento.page/slides/
894•starfallg•21h ago•200 comments

GigaToken: ~1000x faster Language model tokenization

https://github.com/marcelroed/gigatoken/
540•syrusakbary•19h ago•113 comments

Worse on Purpose – How Corporate Greed Killed Product Quality – Worse on Purpose

https://www.worseonpurpose.com/
69•bilsbie•1h ago•33 comments

Are AI labs pelicanmaxxing?

https://dylancastillo.co/posts/pelicanmaxxing.html
586•dcastm•19h ago•224 comments

So Reddit has decided that plain HTML is unsafe

https://www.cole-k.com/2026/07/21/reddit/
532•montroser•1d ago•534 comments

ANSI escape injection in MCP servers: Hidden from humans, visible to AI

https://brightsec.com/research/detecting-ansi-escape-sequence-injection-in-mcp-servers-with-dast/
32•xgpyc2qp•2d ago•14 comments

Test-time training 3D reconstruction

https://github.com/Inception3D/TTT3R
7•soupspaces•1w ago•0 comments

Protecting our FLOSS commons from LLMs

https://blog.codeberg.org/protecting-our-floss-commons-from-llms.html
103•acmnrs•11h ago•38 comments

Making

https://beej.us/blog/data/ai-making/
387•erikschoster•21h ago•151 comments

Amiga 1000: Ten years ahead of its time

https://dfarq.homeip.net/amiga-1000-ten-years-ahead-of-its-time/
110•giuliomagnifico•7h ago•101 comments

git's –end-of-options Flag

https://nesbitt.io/2026/07/21/end-of-options.html
166•Erenay09•1d ago•99 comments

The startup's Postgres survival guide

https://hatchet.run/blog/postgres-survival-guide
439•abelanger•1d ago•198 comments

Show HN: Cactus Hybrid: We taught Gemma 4 to know when it's wrong

https://github.com/cactus-compute/cactus-hybrid
157•HenryNdubuaku•18h ago•35 comments

Making ASCII Art in Vim

https://alexyang.dev/vim-ascii-art/
83•evakhoury•2d ago•9 comments

John C. Dvorak has died

https://twitter.com/na_announce/status/2079952538040672302
796•coleca•17h ago•266 comments

Malleable Computing, Emacs, and You

http://yummymelon.com/devnull/malleable-computing-emacs-and-you.html
120•kickingvegas•15h ago•34 comments

Businesses with ugly AI menu redesigns

https://blog.fiddery.com/businesses-with-ugly-ai-menu-redesigns/
324•speckx•23h ago•241 comments

Nobody knows what a used GPU cluster is worth

https://ciphertalk.substack.com/p/nobody-knows-what-a-used-gpu-cluster
251•rbanffy•1w ago•226 comments

Medici family mystery may be solved after more than 400 years

https://www.cnn.com/2026/07/15/science/medici-family-mystery-dna-malaria
132•effects•14h ago•40 comments

Publishers consider opting out of Google as search traffic declines

https://www.niemanlab.org/2026/07/search-traffic-has-declined-so-much-that-some-publishers-are-co...
10•giuliomagnifico•1h ago•1 comments

Frequently Asked Questions on Expertise

https://jtpeterson.substack.com/p/faq-on-expertise
15•surprisetalk•2d ago•0 comments