This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Static variables in template functions
nathan@codesourcery.com (Nathan Sidwell) wrote on 17.04.05 in <42622BB2.1000209@codesourcery.com>:
> Mark Mitchell wrote:
>
> > 1. Adopt a coding standard for V3 that forbids static variables with
> > constant initializers in template/inline functions. Downside:
> > painful for V3 developers.
> And also pessimizes systems _with_ weak support. (Though as Paolo
> says, this instance is not performace critical.)
>
>
> > 3. Modify the compiler to initialize such variables dynamically,
> > thereby eliminating the problem. Downside: pessimizes code.
>
> I agree with your reasoning that this is the appropriate solution.
> [Although, in general, the compiler is not permitted to turn static
> initialization into dynamic initialization, I think for such
> function-scope constant initializers, a program cannot tell the
> difference -- but make sure we still treat the variable as a
> constan for the remainder of the function.]
Surely if a program can tell the difference will depend on any side
effects outside the function the initializer has. (If, that is, you init
when first entering the function, instead of treating it like a global
variable and initing at program start.)
Incidentally, that's another potential problem with static initialization,
having such side effects occur more than once.
MfG Kai