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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:19:46+00:00 2026-05-15T22:19:46+00:00

take two following classes and their constructors as samples: class One{ public: One(int a,int

  • 0

take two following classes and their constructors as samples:

class One{
 public:
  One(int a,int b):adad1(a),adad2(b){}
 private:
  int adad1;
  int adad2;
};
class Two{
 public:
  Two(int input[]){
   for (int i=0;i<10;i++)
    araye[i]=input[i];
  }
 private:
  int araye[10];
};

considering objects with static storage duration, I think first constructor can be applied during compile time due to its free function body that allows it to be converted to a constant expression in some cases as an optimization, but I have doubt about second one. anyway is there any rule specifying which kinds of constructors can be applied during compile time ?

  • 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-15T22:19:47+00:00Added an answer on May 15, 2026 at 10:19 pm

    There is no guarantee that any of the two are statically initialized before any runtime code is executed. For the first, it’s easy to make it happen, though

    class One{
     public:
      int adad1;
      int adad2;
    };
    
    // initialized statically, if a and b are constant-expressions
    One one = { a, b }; 
    

    As another guy says, constexpr in C++0x allows constructors to be executed statically. In your case that would work for the first case, but not for the second. You will have to live that for the second, no guarantee is made by the Standard. But the Standard still allows an implementation to optimize it to be done at static initialization phase. See 3.6.2/2

    An implementation is permitted to perform the initialization of an object of namespace scope with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that

    • the dynamic version of the initialization does not change the value of any other object of namespace
      scope with static storage duration prior to its initialization, and
    • the static version of the initialization produces the same value in the initialized object as would be produced by the dynamic initialization if all objects not required to be initialized statically were initialized dynamically.

    If the array given is suitable, your constructor may not violate those rules. The Standard shows an example, which i explained in more detail here. For completion, the example code is shown below

    inline double fd() { return 1.0; }
    extern double d1;
    double d2 = d1;   // unspecified:
                      // may be statically initialized to 0.0 or
                      // dynamically initialized to 1.0
    
    double d1 = fd(); // may be initialized statically to 1.0
    

    As you see, putting things to be initialized earlier can even go with changed initial values if there exist a certain relation between variables.

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

Sidebar

Related Questions

take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
Suppose we have following two classes: class Temp{ public: char a; char b; };
I've got the following two classes in C#: public class MyFirstClass : IMyFirstClass {
I have the following two functions which take two dictionaries and recursively add their
Jersey offers two classes to interact with annotations on resources: ResourceFilterFactory , one class
Take the following two lines of code: for (int i = 0; i <
Imagine the following scenario: I have two classes in a one-to-many relationship and I
I'm having a problem with polymorphism and parent-child relationships. Take the following two classes,
Take these two classes for instance: struct Owned { Owned() : i() { }
I have the opportunity to take a two day class on Perl 6 with

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.