C++ Logo

std-proposals

Advanced search

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

From: Walt Karas <wkaras_at_[hidden]>
Date: Fri, 3 Oct 2025 17:22:44 +0000 (UTC)
On Friday, October 3, 2025 at 05:05:10 AM EDT, Avi Kivity <avi_at_[hidden]> wrote:

On Fri, 2025-10-03 at 01:24 +0000, Walt Karas via Std-Proposals wrote:
> On Thursday, October 2, 2025 at 06:21:33 PM EDT, Jeremy Rifkin via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Hi,
>
> What's the motivation for non-static thread-local class members?
>
> What's the motivation for restricting an instance of a class to have
> static storage duration?
>
> Answer: Class versus per-instance member variable seems fully orthogonal to shared-by-all-threads versus thread-local, logically speaking. But a lot of complex scenarios arise if the thread lifetime is not contained within the lifetime an object that has thread_local variables as non-static members. Only allowing per-instance thread_local member variables avoids that complexity.


If it was truly orthogonal, then you could have non-static instances of this class that has non-static thread-local variables.

Response: The motivation for it is orthogonal. A non-static object could have use for the values of a (non-static) thread_local member variable, in threads whose lifetimes are contained within the lifetime of the object. But the implementation is clearly not orthogonal. It's primarily due to implementation issue that non-static thread_local members should be limited to objects with program lifetime.

Received on 2025-10-03 17:22:53