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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:07:44+00:00 2026-06-13T16:07:44+00:00

Global variables in C++11 with non-trivial constructors are constructed before the entry to main

  • 0

Global variables in C++11 with non-trivial constructors are constructed before the entry to main during a static initialization phase.

Likewise non-function-local thread_local variables are constructed during a per-thread “thread_local initialization phase”.

Does the C++11 standard specify in what order these variables shall be constructed? In both cases if there are two variables:

// global scope

A::A() { b.f(); }  // A constructor uses global b

A a;
B b;

Does the C++11 standard specify in what order they shall be initialized, or that an error should be produced if a variable is used uninitialized?

Likewise for non-function-local thread_local:

// global scope

A::A() { b.f(); }  // A constructor uses global b

thread_local A a;
thread_local B b;

Does the standard specify the order they must be constructed, and does it define what will happen if the variable is used from the constructor of another before it is initialized?

Can you please provide a C++11 standard reference in support of any claims you make to have an answer.

  • 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-13T16:07:45+00:00Added an answer on June 13, 2026 at 4:07 pm

    Your statement that “Global variables in C++11 with non-trivial constructors are constructed before the entry to main during a static initialization phase.” doesn’t seem to be entirely true – they may not be initialised until the dynamic initialization phase

    For variables with “ordered initialization”, which your first a and b are, then the standard says

    Variables with ordered initialization defined within a single
    translation unit shall be initialized in the order of their
    definitions in the translation unit.

    3.6.2/2 covers all this.

    Edit: as far as I can tell your second a and b don’t have ordered initialization, and could be initialized in either order. But I may be missing something.

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

Sidebar

Related Questions

I can't find a good explanation about global non static variables in unnamed namespace.
Is there a way to manipulate non-global variables from a fileevent handler? Consider the
I've subclass Application to keep a global state in static variables. But if the
I'd like to do some stuffs before main function. I have multiple source files.
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
The discussion around global variables and their misuse seems to hold a certain dogmatic
I always hear using global variables are dangerous. Does this apply to Drupal? Take
I'm using global variables to pass around a response from a AJAX call: window.response
Possible Duplicate: Global variables in R ad the beginning of the file I wrote:
I have a global variables that I am using as a default variable. Depending

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.