Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 677551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:03:07+00:00 2026-05-14T01:03:07+00:00

I have a class with static method which has a local static variable. I

  • 0

I have a class with static method which has a local static variable. I want that variable to be computed/evaluated once (the 1st time I call the function) and for any subsequent invocation, it is not evaluated anymore. How to do that? Here’s my class:

template<
    typename T1 = int, unsigned N1 = 1,
    typename T2 = int, unsigned N2 = 0,
    typename T3 = int, unsigned N3 = 0,
    typename T4 = int, unsigned N4 = 0,
    typename T5 = int, unsigned N5 = 0,
    typename T6 = int, unsigned N6 = 0,
    typename T7 = int, unsigned N7 = 0,
    typename T8 = int, unsigned N8 = 0,
    typename T9 = int, unsigned N9 = 0,
    typename T10 = int, unsigned N10 = 0,
    typename T11 = int, unsigned N11 = 0,
    typename T12 = int, unsigned N12 = 0,
    typename T13 = int, unsigned N13 = 0,
    typename T14 = int, unsigned N14 = 0,
    typename T15 = int, unsigned N15 = 0,
    typename T16 = int, unsigned N16 = 0>
struct GroupAlloc
{
    static const uint32_t sizeClass;
    static uint32_t getSize()
    {
        static uint32_t totalSize = 0;

        totalSize += sizeof(T1)*N1;
        totalSize += sizeof(T2)*N2;
        totalSize += sizeof(T3)*N3;
        totalSize += sizeof(T4)*N4;

        totalSize += sizeof(T5)*N5;
        totalSize += sizeof(T6)*N6;
        totalSize += sizeof(T7)*N7;
        totalSize += sizeof(T8)*N8;

        totalSize += sizeof(T9)*N9;
        totalSize += sizeof(T10)*N10;
        totalSize += sizeof(T11)*N11;
        totalSize += sizeof(T12)*N12;

        totalSize += sizeof(T13)*N13;
        totalSize += sizeof(T14)*N14;
        totalSize += sizeof(T15)*N15;
        totalSize += sizeof(T16)*N16;

        totalSize = 8*((totalSize + 7)/8);

        return totalSize;
    }
};

EDIT:

Thanks all for your prompt help. +1 to everyone. I chose Tyler McHenry’s answer because it does not need any comparison, purely static function evaluation. I will need this code for allocator so avoiding another “if” should be better. Thanks again!

EDIT:

gf’s answer turned out to be the best one as it deals with assignment during compile-time and saves the program from thread-safe headache and explicit initialization. However, I respect the previous best answer. I will give credit here instead of changing the tick mark. Thanks everyone for helping!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:03:07+00:00Added an answer on May 14, 2026 at 1:03 am

    Make another static function that does the computation, and use that for the initialization of the variable, e.g.

    static uint32_t computeSize() 
    {
      uint32_t init_totalSize;
    
      // Lots of code
    
      return init_totalSize;
    }
    
    static uint32_t getSize()
    {
      static uint32_t totalSize = computeSize();
      return totalSize;
    }
    

    Static variables are guaranteed to be initialized exactly once (the first time the function containing them is used).

    Edit: But this is not thread-safe. This page explains why in great detail.

    To make it thread-safe, it is not sufficient to wrap the initialization of totalSize (the call to computeSize) in a critical section, because static variable initialization is “compiler magic”, and it can be that the variable to undergoes initialization at any time during the call to getSize before it is used, even before the function’s first statement. What you need to do is prevent more than one thread from even calling getSize at the same time, which can be accomplished with yet another level of indirection, e.g.

    static uint32_t computeSize() 
    {
      uint32_t init_totalSize;
    
      // Lots of code
    
      return init_totalSize;
    }
    
    static uint32_t real_getSize()
    {
      static uint32_t totalSize = computeSize();
      return totalSize;
    }
    
    static uint32_t getSize()
    {
      uint32_t totalSize;
      /* --- Enter Critical Section (acquire lock) -- */
      totalSize = real_getSize();
      /* --- Exit Critical Section (release lock) -- */
      return totalSize;
    }
    

    This prevents two threads from even entering the function that contains the static variable at the same time, and ensure that its initialization will occur within a critical section.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your HTML markup is unclear but you can hide all… May 15, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Moo, I fought with this one for quite a while… May 15, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer This is an optimization performed at execution-time. Although the call… May 15, 2026 at 5:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.