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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:29:51+00:00 2026-06-17T17:29:51+00:00

As far as I know the constructors of global objects need to be called

  • 0

As far as I know the constructors of global objects need to be called before anybody tries using those objects. However in my program this doesn’t seem to be the case. Here is my simplified code (I compile it with gcc version 4.6.3-1ubuntu5)

#include <iostream>

using namespace std;

struct type_data
{
    int id;

    type_data()
        : id(-1) // set some invalid id
    {
        cout << "creating a new type data" << endl;
    }
};

template <typename T>
struct type_data_for_type
{
    static type_data data;
};
template <typename T>
type_data type_data_for_type<T>::data;


struct type_registry
{
    static type_registry& instance()
    {
        static type_registry i;
        return i;
    }

    void register_type(type_data& t)
    {
        cout << "registering a type" << endl;
        t.id = last_id++;
    }

    int last_id;
};

template <typename T>
struct registrator
{
    registrator()
    {
        type_registry::instance().
            register_type(type_data_for_type<T>::data);
    }

    int unused;
    static registrator payload;
};
template <typename T>
registrator<T> registrator<T>::payload;

class foo {};

inline void register_foo()
{
    registrator<foo>::payload.unused = 1;
}

int main()
{
    cout << type_registry::instance().last_id << endl;
    cout << type_data_for_type<foo>::data.id << endl;
    return 0;
}

Basically it registers the type foo globally via register_foo. I expect the output to be:

creating a new type data
registering a type
1
0

But instead it is:

registering a type
creating a new type data
1
-1

This means that i’ve set the id of a type_data object before its constructor was called.

So, is this a compiler bug? Am I missing something? Can I make this work?

  • 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-17T17:29:53+00:00Added an answer on June 17, 2026 at 5:29 pm

    I believe you cannot rely on the order of initialization of static member variables of class templates in this case.

    Relevant here is § 3.6.2/2 of the C++ Standard:

    *”Dynamic initialization of a non-local variable with static storage duration 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.”

    And of course also § 9.4.2/6, which refers the above rule:

    “Static data members are initialized and destroyed exactly like non-local variables (3.6.2, 3.6.3).”

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

Sidebar

Related Questions

As far as I have used converting constructors they look like this: struct X
So far I know that FileSystemWatcher can look into a folder and if any
As far as know, I must be careful with PHP, and I think Javascript.
as far as I know both 2012-07-04T17:30:52+00:00 and 2012-07-04T17:30:52Z are dates in iso8601 format
As far as I know there is no way to migrate existing Facebook comments
As far as I know, C2DM is per app per device thing..but my question
As far as I know, Timer runs asynchronously, so If a very short duration
As far as I know that JSF keeps all the session scoped bean in
As far as I know, VisualSVN Server is a server while TortoiseSVN is a
as far as i know there are cross domain restictions that wont allow you

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.