C++ Logo

std-proposals

Advanced search

Re: [std-proposals] static static variable inside template function

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 04 Oct 2023 10:47:00 -0700
On Tuesday, 3 October 2023 10:17:21 PDT Frederick Virchanza Gotham via Std-
Proposals wrote:
> How about if we could specify inside a template function that a
> variable is 'static static', meaning that it's shared by all
> instantiations? So the following code:

Just move it to another function.

    namespace detail {
        std::string &string_cache()
        {
            static std::string str(arg, 'a');
            return str;
        }
    }
    template<typename T>
    T *Func(unsigned const arg)
    {
        std::string &str = detail::string_cache();
        /* other stuff here */
        return nullptr;
    }

Since this is already possible, please explain why this is too difficult /
cumbersome to use and what other advantages there would be.
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-10-04 17:47:06