C++ Logo

std-proposals

Advanced search

[std-proposals] Suggestion: non-static member variables for static-duration-only classes

From: Walt Karas <wkaras_at_[hidden]>
Date: Thu, 2 Oct 2025 20:57:31 +0000 (UTC)
I suggest allowing non-static thread_local member variables for classes, which would implicitly restrict instances of the class to have static storage duration.

To support this change, I suggest adding the attribute [[static_duration]], for use in class declarations. If a class is declared with this attribute, all of its instances must have static duration. If a class without this attribute has a non-static thread_local member variable, a warning should be issued. (A warning should also be issued if the class has a base class or member variable whose type has implicit or explicit [[static_duration]].)

This would have been nice to have for a class I'm currently working on. The only work-around I could to come up with was to make it a class template where the instantiations are singletons. The template takes a single (class) parameter. The only purpose of the template parameter is to create distinct instantiations, with distinct thread_local member variables.

Received on 2025-10-02 20:59:56