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

  • SEARCH
  • Home
  • 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 7745051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:55:01+00:00 2026-06-01T09:55:01+00:00

// File: InitFirst.h #pragma once template <int val> struct InitFirst { static float s_dividedByThree;

  • 0
// File: InitFirst.h

#pragma once

template <int val>
struct InitFirst
{
    static float s_dividedByThree;
};

template <int val>
float InitFirst<val>::s_dividedByThree = val / 3.0;

// File: Test.h

#include <conio.h>
#include <tchar.h>

#include "InitFirst.h"

float g_shouldBeOneThird = InitFirst<1>::s_dividedByThree;

int _tmain(int argc, _TCHAR* argv[])
{
    _cprintf("%f\n", g_shouldBeOneThird);
    getch();
    return 0;
}

Is g_shouldBeOneThird guaranteed to be initialized to around 0.333? In other words, is the statically initialized InitFirst<1>::s_dividedByThree guaranteed to be initialized by the time it’s used for statically initializing g_shouldBeOneThird?

  • 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-06-01T09:55:02+00:00Added an answer on June 1, 2026 at 9:55 am

    From the standard (3.6.2):

    Objects with static storage duration (3.7.1) shall be zero-initialized
    (8.5) before any other initialization takes place. A reference with
    static storage duration and an object of POD type with static storage
    duration can be initialized with a constant expression (5.19); this is
    called constant initialization. Together, zero-initialization and
    constant initialization are called static initialization; all other
    initialization is dynamic initialization. Static initialization shall
    be performed before any dynamic initialization takes place. Dynamic
    initialization of an object is either ordered or unordered.
    Definitions of explicitly specialized class template static data
    members have ordered initialization. Other class template static data
    members (i.e., implicitly or explicitly instantiated specializations)
    have unordered initialization. Other objects defined in namespace
    scope have ordered initialization. Objects defined within a single
    translation unit and with ordered initialization shall be initialized
    in the order of their definitions in the translation unit. The order
    of initialization is unspecified for objects with unordered
    initialization and for objects defined in different translation units.

    In your case here, since you’re initializing float InitFirst<val>::s_dividedByThree with a constant expression, this will happen before any dynamic initialization (f.x float g_shouldBeOneThird). Though I have a feeling this simplified example might be a simplification of a case where you have dynamic initialization, then the relevant part is: “Objects defined within a single translation unit and with ordered initialization shall be initialized in the order of their definitions in the translation unit.”.

    There is a trick to make sure that global variables (sort of) are initialised by the time you use them. The trick is to keep them as a static local variable in a global function. Since local static variables are initialised the first time they’re accessed, initialisation order is not an issue anymore:

    template <int val>
    struct InitFirst
    {
        static float & s_dividedByThree();
    };
    
    template <int val>
    float & InitFirst<val>::s_dividedByThree(){
        static float staticVariable = val / 3.0;
        return staticVariable;
    }
    

    You can then access those variables almost as before:

    float g_shouldBeOneThird = InitFirst<1>::s_dividedByThree();
    

    Beware though, initialization of local static variables are not safe under multiple threads (it’s not in the standard that they should be safe). If that is a concern for you, you might want to protect the initializations with
    some locks. The compilers are of course allowed to generate safe code, which is what gcc does by default (probably others too).

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

Sidebar

Related Questions

// File: foo.c static int var; void foo() { var++; } // end of
File ex1.hpp (definition of template class): #ifndef EX1_HPP #define EX1_HPP #include ex2.hpp template <class
file a.cc int a = 0; file b.cc #include a.cc file main.cc #include b.cc
FILE *fp; fp=fopen(c:\\test.txt, r); int fgetc (FILE *fp); int fputc( int c, FILE *fp
File api.h #include <stdio.h> #ifndef API #define API struct trytag; typedef struct trytag try;
File 1: static char* const path; //GLOBAL int main() { path = FunctionReturningPath(); UsePath()
File name: widgets.c #include <QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv);
File formats I would like to play include .wav, .mp3, .midi. I have tried
FILE *out=fopen64(text.txt,w+); unsigned int write; char *outbuf=new char[write]; //fill outbuf printf(%i\n,ftello64(out)); fwrite(outbuf,sizeof(char),write,out); printf(%i\n,write); printf(%i\n,ftello64(out));
FILE #1 (foo.h): #ifndef FOO_H_ #define FOO_H_ #include baseclass.h #include bar.h class Bar; class

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.