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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:28:50+00:00 2026-05-12T00:28:50+00:00

Within the same compilation unit, the C++ standard says that static initialization order is

  • 0

Within the same compilation unit, the C++ standard says that static initialization order is well defined — it’s the order of the declarations of the static objects. But using the Sun Studio 12 compiler I’m encountering unintuitive behavior. I’ve define a templated class helper<T> which contains a static member _data of type T and a static member function that uses _data called foo. In my .cpp file I have this above main():

struct A { /* some definition */ };

typedef helper<int> s0;
typedef helper<A> s1;

Notice that the typedef for helper<int> comes before the typedef for helper<A>. Thus according to the standard I would expect that helper<int>::_data will be constructed before helper<A>::_data (remember _data is a static member). On GCC this is the case, on Sun it is not.

This is problematic because A’s constructor uses helper<int>::_data. I only have one compilation unit, with no earlier potential instantiation of helper<A>, so I thought the order should be well defined. Is this a Sun compiler bug, or does the typedef not constitute a definition/instantiation technically? What I mean is, is the Sun compiler’s behavior allowed by the standard?

I have the following main():

int main()
{
    //Swapping the order of these has no effect on Sun
    s0::foo();
    s1::foo();
}

There are no other uses of s0 or s1.

  • 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-12T00:28:51+00:00Added an answer on May 12, 2026 at 12:28 am

    Within the same compilation unit, the C++ standard says that static initialization order is well defined — it’s the order of the declarations of the static objects.

    In your shown code you have no declaration of a static data member. You have a declaration of a typedef-name. These have nothing to do with that, and don’t influence any order. You probably think along this way:

    If i make that typedef declaration, it will instantiate helper<int>, and thus instantiate its static data member declaration first.

    The problem is, that line does not cause an instantiation of helper<int>. For that to happen, you would need an explicit instantiation or manage to make it instantiate it implicitly (creating an object of helper<int> for example, or using it as a nested name specifier as in helper<int>::... and explicitly referencing the static member – otherwise, creation of it is omitted).

    But there is a much deeper problem. The order is not the declaration of the static data-members. The order is their definition. Consider the following

    struct C { C() { printf("hey\n"); } };
    struct A { 
      static C a;
      static C b;
    };
    
    C A::b;
    C A::a;
    

    In this code, b is created before a, even though a is declared before b.

    The following code prints 2 1:

    struct C { C(int n) { printf("%d\n", n); } };
    
    template<int N>
    struct A {
      static C c;
    };
    
    template<int N>
    C A<N>::c(N);
    
    // explicit instantiation of declaration and definition
    template struct A<2>;
    template struct A<1>;
    
    int main() {
    
    }
    

    But the following code prints nothing, unless you comment in the line in main.

    struct C { C(int n) { printf("%d\n", n); } };
    
    template<int N>
    struct A {
      static C c;
    };
    
    template<int N>
    C A<N>::c(N);
    
    // implicit instantiation of declarations
    A<2> a2;
    A<1> a1;
    
    int main() {
      // A<1>::c; A<2>::c;
    }
    

    I’m not actually sure what the correct output for this second snippet is. Reading the Standard, i can’t determine an order. It says at 14.6.4.1 “Point of Instantiation”:

    For a function template specialization, a member function template specialization, or a specialization for a member function or static data member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization […]. Otherwise, the point of instantiation for such a specialization immediately follows the namespace scope declaration or definition that refers to the specialization.

    The point of instantiation of their definitions both appear immediately after the definition of main. Which definition is instantiated before the other definition seems to be left unspecified. If anyone knows the answer and khow other compilers behave (GCC prints 1 2 but with the order of the expressions in main swapped, prints 2 1), please let me know in the comment.

    For details, see this answer about static object’s lifetime.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Aside from hosting in a browser control or window, there's… May 12, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer How about this MSDN article: Order of events in Windows… May 12, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer In a related question I had some time ago, there… May 12, 2026 at 6:20 am

Related Questions

I'm learning Objective-C, and have a C/C++ background. In object-oriented C++, you always need
I've been trying to put a QX11EmbedContainer in my app, and I need to
Simply querying running jobs using something like select * from dba_jobs_running; works fine when
i am using Solaris 10 OS(x86). i installed beanstalkd and it starts fine by

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.