logoalt Hacker News

fancy_pantsertoday at 3:48 PM2 repliesview on HN

The compiler can substitute the value how it sees fit. It's like #define, but type-safe and scoped.

Maybe it's folded into expressions, propagated through constant expressions, or used it in contexts that require compile-time constants (template parameters, array sizes, static_assert, other constexpr expressions).

I mean, not in this case of pi/2, where it's more about announcing semantics, but in general those are the purposes and uses.


Replies

gzreadtoday at 4:39 PM

It can do this with const too or even a normal variable that just happens to not vary.

show 1 reply
ErroneousBoshtoday at 6:07 PM

I'd like something like this in C or C++ quite honestly.

Something like a struct that I can say "this struct is global to the whole program and everyone can see it, but once this function exits those values are locked in". Maybe something like that one function is allowed to unlock and update it, but nowhere else.

Think in terms of storing a bunch of precomputed coefficients that are based on the samplerate of a system, where you really only need to set it up once on startup and it is unlikely to change during the application's running lifetime.

I feel like there probably is a way to do this, and if I was good at high level languages like C I'd know what it is. If you know, tell me what I'm not understanding ;-)

show 1 reply