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•6mo ago

Comments

wahern•6mo 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•6mo 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•6mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•6mo 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•6mo 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•6mo 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•6mo ago
Learning such a language doesn’t mean I can use it.
o11c•6mo 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•6mo 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•6mo 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•6mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•6mo ago
It still is a thing, though.
cperciva•6mo 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•6mo 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.

The privacy nightmare of browser fingerprinting

https://kevinboone.me/fingerprinting.html
59•ingve•47m ago•28 comments

Agent design is still hard

https://lucumr.pocoo.org/2025/11/21/agents-are-hard/
207•the_mitsuhiko•6h ago•103 comments

In a U.S. First, New Mexico Opens Doors to Free Child Care for All

https://www.wsj.com/us-news/in-a-u-s-first-new-mexico-opens-doors-to-free-child-care-for-all-2dfd...
159•nairteashop•1h ago•128 comments

Personal blogs are back, should niche blogs be next?

https://disassociated.com/personal-blogs-back-niche-blogs-next/
497•gnabgib•19h ago•325 comments

Helping Valve to power up Steam devices

https://www.igalia.com/2025/11/helpingvalve.html
725•TingPing•1d ago•260 comments

As 'Dorian Gray' ages, its relevance only grows

https://www.washingtonpost.com/books/2025/11/08/dorian-gray-oscar-wilde-history/
10•apollinaire•1w ago•1 comments

How to See the Dead

https://www.asimov.press/p/see-the-dead
31•mailyk•5d ago•1 comments

Samsung's 60% DRAM price hike signals a new phase of global memory tightening

https://www.buysellram.com/blog/samsungs-memory-price-surge-sends-shockwaves-through-the-global-d...
382•redohmy•1w ago•322 comments

The twin probes just launched toward Mars have an Easter egg on board

https://arstechnica.com/space/2025/11/the-twin-probes-just-launched-toward-mars-have-an-easter-eg...
43•pseudolus•1w ago•23 comments

'The French people want to save us': help pours in for glassmaker Duralex

https://www.theguardian.com/world/2025/nov/22/french-people-want-to-save-us-help-pours-glassmaker...
56•n1b0m•2h ago•16 comments

TiDAR: Think in Diffusion, Talk in Autoregression

https://arxiv.org/abs/2511.08923
53•internetguy•1w ago•5 comments

Show HN: Wealthfolio 2.0- Open source investment tracker. Now Mobile and Docker

https://wealthfolio.app/?v=2.0
597•a-fadil•1d ago•192 comments

Weight-sparse transformers have interpretable circuits [pdf]

https://cdn.openai.com/pdf/41df8f28-d4ef-43e9-aed2-823f9393e470/circuit-sparsity-paper.pdf
57•0x79de•1w ago•14 comments

The Connectivity Standards Alliance Announces Zigbee 4.0 and Suzi

https://csa-iot.org/newsroom/the-connectivity-standards-alliance-announces-zigbee-4-0-and-suzi-em...
90•paulatreides•4d ago•58 comments

Moss Survives 9 Months in Space Vacuum

https://scienceclock.com/moss-survives-9-months-in-space-vacuum/
129•ashishgupta2209•13h ago•51 comments

Original Superman comic becomes the highest-priced comic book ever sold

https://www.bbc.com/news/articles/c8e9rp0knj6o
290•1659447091•12h ago•183 comments

How I learned Vulkan and wrote a small game engine with it (2024)

https://edw.is/learning-vulkan/
151•jakogut•18h ago•80 comments

Sharper MRI scans may be on horizon thanks to new physics-based model

https://news.rice.edu/news/2025/sharper-mri-scans-may-be-horizon-thanks-new-physics-based-model
119•hhs•17h ago•31 comments

We should all be using dependency cooldowns

https://blog.yossarian.net/2025/11/21/We-should-all-be-using-dependency-cooldowns
419•todsacerdoti•1d ago•250 comments

Kodak ran a nuclear device in its basement for decades

https://www.popularmechanics.com/science/energy/a69147321/kodak-film-nuclear-reactor/
183•cainxinth•1w ago•119 comments

Our babies were taken after 'biased' parenting test

https://www.bbc.co.uk/news/articles/c1wlw2qj113o
21•binning•1h ago•20 comments

New Apple Study Shows LLMs Can Tell What You're Doing from Audio and Motion Data

https://9to5mac.com/2025/11/21/apple-research-llm-study-audio-motion-activity/
19•andrewrn•2h ago•7 comments

LAPD helicopter tracker with real-time operating costs

https://lapdhelicoptertracker.com/
200•polalavik•19h ago•234 comments

Arduino Terms of Service and Privacy Policy update: setting the record straight

https://blog.arduino.cc/2025/11/21/the-arduino-terms-of-service-and-privacy-policy-update-setting...
77•manchoz•20h ago•72 comments

Concrete Shipbuilding – Argentina

https://thecretefleet.com/blog/f/concrete-shipbuilding-–-argentina
44•surprisetalk•5d ago•11 comments

Childhood Friends, Not Moms, Shape Attachment Styles Most

https://nautil.us/childhood-friends-not-moms-shape-attachment-styles-most-1247316/
253•dnetesn•1w ago•87 comments

Discontinuation of ARM Notebook with Snapdragon X Elite SoC

https://www.tuxedocomputers.com/en/Discontinuation-of-ARM-notebooks-with-Snapdragon-X-Elite-SoC.t...
183•Venn1•22h ago•116 comments

You can make PS2 games in JavaScript

https://jslegenddev.substack.com/p/you-can-now-make-ps2-games-in-javascript
296•tosh•1d ago•71 comments

Pixar: The Early Days A never-before-seen 1996 interview

https://stevejobsarchive.com/stories/pixar-early-days
148•sanj•21h ago•13 comments

Is Matrix Multiplication Ugly?

https://mathenchant.wordpress.com/2025/11/21/is-matrix-multiplication-ugly/
126•jamespropp•19h ago•89 comments