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 6141579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:16:12+00:00 2026-05-23T18:16:12+00:00

In a given file if I have, struct A { static int a; };

  • 0

In a given file if I have,

struct A { static int a; };
struct B { static int b; };
int A::a;
int B::b;

Then, I can always expect that A::a gets initialized before B::b. Now for the same file, take the template case,

template<typename T>
struct X { static T t; };
template<typename T>
T X<T>::t;

Suppose, X is instantiated with A and B and its static member is used arbitrarily somewhere in the code as, X<A>::t and X<B>::t, then what should be the order for initialization of template static member X<T>::t; ? Is it well defined ?

  • 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-23T18:16:13+00:00Added an answer on May 23, 2026 at 6:16 pm

    As long as the templates only have one definition (e.g. you only have one translation unit), it’s well-defined. The static members are initialized in the order that the template specializations are instantiated in contexts that require the definition of the static data member. From §14.7.1/1 [temp.inst] of the C++03 standard (emphasis mine):

    Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program. The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates; and it causes the implicit instantiation of the definitions of member anonymous unions. Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

    §14.7.1/7 also states:

    The implicit instantiation of a class template does not cause any static data members of that class to be implicitly instantiated.

    However, things get more complicated when you have multiple translation units that define the template. §3.2/5 [basic.def.odr] states:

    There can be more than one definition of a class type (clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (clause 14), non-static function template (14.5.5), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.4) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

    (list of conditions…)

    If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

    Note that the standard doesn’t specify which definition is taken as the single definition, only that some definition is chosen. So, if multiple translation units instantiate the templates in different orders, there’s no guarantee what the order of initialization will be.

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

Sidebar

Related Questions

Given I have two File objects I can think of the following implementation: public
I have a Windows batch file that processes all the files in a given
I have code in my header file that looks like: typedef struct _bn bnode;
I have to search a given file name (let say Keyword) in a directory
I have a functionality where a user is given file to download. It works
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
I have been given a WSDL file and I need to consume a web
I have a data structure which is as given below: class File { public
I'm working on a program that searches entire drives for a given file. At
I have the following perl script that works locally given the input parameters. I

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.