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•7mo 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.

Modern Cassette Walkmans

https://walkman.land/modern
42•classichasclass•39m ago•3 comments

The universal weight subspace hypothesis

https://arxiv.org/abs/2512.05117
188•lukeplato•5h ago•69 comments

The web runs on tolerance

https://shkspr.mobi/blog/2025/12/the-web-runs-on-tolerance/
53•speckx•4d ago•45 comments

Show HN: I built a system for active note-taking in regular meetings like 1-1s

https://withdocket.com
24•davnicwil•7h ago•2 comments

Icons in Menus Everywhere – Send Help

https://blog.jim-nielsen.com/2025/icons-in-menus/
329•ArmageddonIt•9h ago•130 comments

Kroger acknowledges that its bet on robotics went too far

https://www.grocerydive.com/news/kroger-ocado-close-automated-fulfillment-centers-robotics-grocer...
110•JumpCrisscross•5h ago•97 comments

Jepsen: NATS 2.12.1

https://jepsen.io/analyses/nats-2.12.1
318•aphyr•10h ago•115 comments

Horses: AI progress is steady. Human equivalence is sudden

https://andyljones.com/posts/horses.html
260•pbui•5h ago•173 comments

The Lost Machine Automats and Self-Service Cafeterias of NYC (2023)

https://www.untappedcities.com/automats-cafeterias-nyc/
47•walterbell•4h ago•17 comments

Strong earthquake hits northern Japan, tsunami warning issued

https://www3.nhk.or.jp/nhkworld/en/news/20251209_02/
277•lattis•14h ago•139 comments

OSHW: Small tablet based on RK3568 and AMOLED screen

https://oshwhub.com/oglggc/rui-xin-wei-rk3568-si-ceng-jia-li-chuang-mian-fei-gong-yi
45•thenthenthen•5d ago•12 comments

AMD GPU Debugger

https://thegeeko.me/blog/amd-gpu-debugging/
225•ibobev•13h ago•36 comments

Let's put Tailscale on a jailbroken Kindle

https://tailscale.com/blog/tailscale-jailbroken-kindle
253•Quizzical4230•13h ago•61 comments

Microsoft increases Office 365 and Microsoft 365 license prices

https://office365itpros.com/2025/12/08/microsoft-365-pricing-increase/
299•taubek•15h ago•358 comments

Scientific and Technical Amateur Radio

https://destevez.net/
38•gballan•4h ago•3 comments

IBM to acquire Confluent

https://www.confluent.io/blog/ibm-to-acquire-confluent/
366•abd12•15h ago•293 comments

Hunting for North Korean Fiber Optic Cables

https://nkinternet.com/2025/12/08/hunting-for-north-korean-fiber-optic-cables/
234•Bezod•12h ago•71 comments

Has the cost of building software dropped 90%?

https://martinalderson.com/posts/has-the-cost-of-software-just-dropped-90-percent/
213•martinald•10h ago•363 comments

Trials avoid high risk patients and underestimate drug harms

https://www.nber.org/papers/w34534
89•bikenaga•10h ago•32 comments

Cassette tapes are making a comeback?

https://theconversation.com/cassette-tapes-are-making-a-comeback-yes-really-268108
58•devonnull•5d ago•91 comments

Everything that is wrong in museums starts with wall labels

https://www.aaronland.info/weblog/2025/11/20/cafeteria/
13•panic•6d ago•8 comments

Paramount launches hostile bid for Warner Bros

https://www.cnbc.com/2025/12/08/paramount-skydance-hostile-bid-wbd-netflix.html
276•gniting•15h ago•268 comments

Show HN: Fanfa – Interactive and animated Mermaid diagrams

https://fanfa.dev/
80•bairess•4d ago•16 comments

AI should only run as fast as we can catch up

https://higashi.blog/2025/12/07/ai-verification/
130•yuedongze•11h ago•123 comments

Latency Profiling in Python: From Code Bottlenecks to Observability

https://quant.engineering/latency-profiling-in-python.html
24•rundef•6d ago•5 comments

Microsoft Download Center Archive

https://legacyupdate.net/download-center/
136•luu•3d ago•17 comments

A series of tricks and techniques I learned doing tiny GLSL demos

https://blog.pkh.me/p/48-a-series-of-tricks-and-techniques-i-learned-doing-tiny-glsl-demos.html
156•ibobev•12h ago•18 comments

Launch HN: Nia (YC S25) – Give better context to coding agents

https://www.trynia.ai/
96•jellyotsiro•12h ago•70 comments

Nova Programming Language

https://nova-lang.net
96•surprisetalk•14h ago•50 comments

Legion Health (YC S21) is hiring a founding engineer (SF, in-person)

1•the_danny_g•12h ago