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.

Kobo can run apps now

https://bandarlabs.github.io/Cobalt/
202•thepoet•3h ago•81 comments

Felony Bench

https://www.felonybench.com/
239•colinprince•4h ago•111 comments

Scientists release biggest 2D map of the universe

https://newscenter.lbl.gov/2026/08/10/scientists-release-biggest-2d-map-of-the-universe/
32•NKosmatos•1h ago•4 comments

Kagi added a setting for removing paywalled links from search results

https://kagi.com/changelog#11296
818•speckx•5h ago•283 comments

AI boosted homework scores, then exam scores dropped: study

https://www.economist.com/graphic-detail/2026/08/18/does-ai-stop-children-from-learning
105•dash2•2d ago•160 comments

I accidentally logged hundreds of thousands of phone calls to military bases

https://lina.sh/blog/hijacking-e164-arpa
292•gavide•6h ago•37 comments

I ran Photoshop on a £0.60 computer chip

https://pointinthecloud.com/2026-08-19-144600.html
54•colinprince•4h ago•8 comments

How we made a text-to-speech model respond in sub-50 ms

https://nari-labs.com/blog/qwen3-tts-speed-cost-frontier/
47•toebee•3h ago•10 comments

Felony charges for citizen deleting phone data at US Border

https://www.nytimes.com/2026/08/21/us/politics/samuel-tunick-deleted-phone-felony.html
132•floathub•7h ago•219 comments

DeepSeek-v4-flash-vision-exp

https://api-docs.deepseek.com/guides/vision/
397•dares2573•9h ago•135 comments

LiteLLM (YC W23) Is Hiring – Rust / Performance Engineers

https://jobs.ashbyhq.com/litellm/3f326076-7415-46a1-921e-8a1b1d6ee2b6
1•ij23•2h ago

People of ACM – Russ Cox

https://www.acm.org/articles/people-of-acm/2026/russ-cox
21•signa11•4d ago•2 comments

A look under our trunk: what's in our compute

https://waymo.com/blog/2026/08/look-under-our-trunk/
34•ra7•1d ago•4 comments

Rebuilding our Electron meeting-recording engine in Swift

https://circleback.ai/blog/how-we-rebuilt-our-electron-recording-engine-in-swift
33•arguiot•2h ago•6 comments

What happens when a GPU reads memory

https://blog.doubleword.ai/what-happens-when-a-gpu-reads-memory
48•ibobev•3h ago•11 comments

Building an (almost) fully self-hosted, sandboxed, agentic software factory

https://blog.jakesaunders.dev/building-an-almost-fully-self-hosted-sandboxed-agentic-software-fac...
35•jakelsaunders94•3h ago•27 comments

Kodak's "pre-invented" lunar orbiter camera; or, the fate of SAMOS readout

https://invertingvision.com/2026/08/10/kodaks-pre-invented-lunar-orbiter-camera-or-the-fate-of-sa...
40•cainxinth•5h ago•1 comments

Show HN: Proliferate- open-source, self-hostable Codex for any coding agent

https://github.com/proliferate-ai/proliferate
29•pablo24602•2h ago•12 comments

Another better lower bound for n=17 square packing

http://gus-massa.blogspot.com/2026/08/another-better-lower-bound-for-n17.html
21•gus_massa•2h ago•2 comments

Decayfmt – a file format that corrupts itself a little every time you open it

https://github.com/aravpanwar/decayfmt
28•unprovable•3h ago•8 comments

New Worlds: We are living in the future of J.G. Ballard or William Gibson

https://precastreinforced.co.uk/2026/08/16/new-worlds/
163•speckx•6h ago•114 comments

The road to ACID transactions in Cassandra 6

https://theconsensus.dev/p/2026/08/16/transactions-in-cassandra.html
46•eatonphil•7h ago•1 comments

I'm becoming AI-blind

https://cymerys.com/w/im-becoming-ai-blind
144•rcymerys•7h ago•137 comments

Show HN: Shoehorn – Quantize any model down to run on your machine

https://notactuallytreyanastasio.github.io/shoehorn/
55•rhgraysonii•3d ago•11 comments

Claudette: Make Claude stop talking like a BuzzFeed article

https://github.com/adnanakil/nobuzz/blob/main/README.md
100•aakil•5h ago•86 comments

The coolest anti-surveillance tools at Defcon [video]

https://www.youtube.com/watch?v=-2uAsJ5EPAw
84•neom•3d ago•5 comments

Quantifying the honey bee dance floor

https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0341456
15•Ariarule•1w ago•1 comments

Radiation damage to Hubble has been 4.3 years out of phase with the Solar cycle

https://arxiv.org/abs/2608.18214
89•pppone•6h ago•30 comments

Galactic Compass 2: now with new augmented reality mode

https://interconnected.org/home/2026/08/21/galactic
8•bobbiechen•2h ago•2 comments

AI companies destroy physical books – let's scan rare books before it's too late

https://annas-archive.gl/blog/physical-destruction.html
435•Cider9986•17h ago•791 comments