C++ Logo

std-discussion

Advanced search

Re: Compile-time Variables and Stateful Meta-programming / STMP

From: SD SH <Z5515zwy_at_[hidden]>
Date: Fri, 11 Jul 2025 17:45:09 +0000
>Variables in functions executed at compile-time need not be constants.
So, are you asking for a variable which exists across translation
units that isn't a compile-time constant?
Right.
>Where compile-time code in
different translation units can read from and modify such variables?
Need more discussion, maybe by defining and using storage class?
>How would that actually work? Like, somebody's got to read it first,
right? Somebody's got to write to it first. Who decides which TU gets
first dibs on the variable? Who decides which TU gets to access it
second?
By implementation. To be honest, it is only able to use in unordered case, for example, I need to register a lot of update functions to a class to execute them (then deside how it executes), we don't pay attention to the order.
>Just look at your example: "count the number of instances of a
template in compile-time". So you have some variable that represents
that instance count. Is that variable somehow a constant expression?
>Because if it is, then you should be able to do `array<T,
instance_count>`. Which is flat-out impossible for a compiler to do
because any TU could increase that count, and a TU's compiler cannot
>reach out and look at every TU that could possibly be used with this
program.
So it needs more constraints, like be only able to use the value after all change or other. (Inneed, it brings many difficulties, that's known, and it is the reason of this discussion. We should try more new design.)


获取Outlook for Android<https://aka.ms/AAb9ysg>

________________________________
发件人: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> 代表 Jason McKesson via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 星期六, 七月 12, 2025 1:18:37 上午
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Jason McKesson <jmckesson_at_[hidden]>
主题: Re: [std-discussion] Compile-time Variables and Stateful Meta-programming / STMP

On Fri, Jul 11, 2025 at 1:07 PM SD SH via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> The topic: Do we need compile-timevariables?
>
> Now C++ has compile-time constants and functions so we can write compile-time code to compute values when compiling code and others, but doesn't approve mutable variables in compile-time.
> Everybody knows that compile-time computing in C++ is functional and stateless then variable states is in opposition to the former.
> In my opinion, stateful compile-time computing is neccessary, because C++ is a OO language, lots of things about types we couldn't do, like type registering, or just simply counting the number of instances of a template. We have many things to do based on that. And I saw some people used non-standard ways to implement some useful libraries (such as Boost.MSM).
> As above, if we need to count the number of instances of a template in compile-time, we can simply use compile-time variables, and not use non-standard ways. Besides, we can implement lots of things we couldn't in the past, instead of putting them into standard. Now is 2025 but we couldn't use all of C++23 yet, so I think we need.

I don't really understand what "compile-time variable" means.

Variables in functions executed at compile-time need not be constants.
So, are you asking for a variable which exists across translation
units that isn't a compile-time constant? Where compile-time code in
different translation units can read from and modify such variables?

How would that actually work? Like, somebody's got to read it first,
right? Somebody's got to write to it first. Who decides which TU gets
first dibs on the variable? Who decides which TU gets to access it
second?

This would be the Static Initialization Order Fiasco, but like way more.

Just look at your example: "count the number of instances of a
template in compile-time". So you have some variable that represents
that instance count. Is that variable somehow a constant expression?
Because if it is, then you should be able to do `array<T,
instance_count>`. Which is flat-out impossible for a compiler to do
because any TU could increase that count, and a TU's compiler cannot
reach out and look at every TU that could possibly be used with this
program.

So it's rather unclear what exactly it is that you're looking for.
--
Std-Discussion mailing list
Std-Discussion_at_lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2025-07-11 17:45:15