C++ Logo

std-discussion

Advanced search

回复: 回复: Compile-time Variables and Stateful Meta-programming / STMP

From: SD SH <Z5515zwy_at_[hidden]>
Date: Sun, 13 Jul 2025 05:29:09 +0000
A compile-time variable (ctv) be declared like this:

// (Assume there is a keyword called ctv)
ctv int i = 0; // must be defined there

Then compiler will allocate sizeof(int) bytes memory to storage it.
ctv in runtime code has constinit.

For safety, a ctv cannot be get the address, and cannot use compile-time new/delete.

consteval int func()
{
    return i++; // OK
}

We can get the value of it at any time, and it shows a state during compiling.
So func() above may returns 1, 2, 3 or other value.

If we need a array with unknown length, we need some new features:

// ctv T&: just like a safe compile-time pointer, and we don't know where it is storaged.
template<typename T>
struct ctarr
{
public:
    template<typename... pTs>
    ctv T& allocate(pTs&&... paras); // allocate and initialize a T by T(paras...)
    // other functions, similar to other containers
};

Then using ranges to iterate it. (ctv ranges returns a ctarr)

********

To compiler:

In compile-time code, a ctv expresed a state,
in runtime code, it is a normal variable.

If a ctv depends is able to be use in single TU, it will be delayed (to collect all changes, similar to LTO, but it is neccessary). (It may spend more time in compiling, compiler can offer a option to ignore computing of ctv in compile-time (postpone until runtime)).

To be honest, it's truly hard to implement, and it almost must breaks the existing compiler architectures.
But I still think we can have a try on it.
________________________________
发件人: Std-Discussion <std-discussion-bounces_at_[hidden]> 代表 Thiago Macieira via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 2025年7月12日 22:34
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Thiago Macieira <thiago_at_[hidden]>
主题: Re: [std-discussion] 回复: Compile-time Variables and Stateful Meta-programming / STMP

On Friday, 11 July 2025 20:40:19 Pacific Daylight Time SD SH via Std-Discussion
wrote:
> All informations are known in compile-time, I think we need a way to
> generate it at that time.

This is what I want you to describe how it would work.

Assume that compilations are done in parallel (make, ninja support that) and
the build takes 3 days with 90,000 files.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Platform & System Engineering



--
Std-Discussion mailing list
Std-Discussion_at_lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2025-07-13 05:29:18