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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:42:26+00:00 2026-06-11T10:42:26+00:00

I am writing some code that has a very large number of reasonably simple

  • 0

I am writing some code that has a very large number of reasonably simple objects and I would like them the be created at compile time. I would think that a compiler would be able to do this, but I have not been able to figure out how.

In C I could do the the following:

#include <stdio.h>

typedef struct data_s {
    int a;
    int b;
    char *c;
} info;

info list[] = {
    1, 2, "a",
    3, 4, "b",
};

main()
{
   int i;
   for (i = 0; i < sizeof(list)/sizeof(*list); i++) {
     printf("%d %s\n", i, list[i].c);
   }
}

Using #C++* each object has it constructor called rather than just being layed out in memory.

#include <iostream>
using std::cout;
using std::endl;

class Info {
    const int a;
    const int b;
    const char *c;
public:
    Info(const int, const int, const char *);
    const int get_a() { return a; };
    const int get_b() { return b; };
    const char *get_c() const { return c; };
};

Info::Info(const int a, const int b, const char *c) : a(a), b(b), c(c) {};

Info list[] = {
    Info(1, 2, "a"),
    Info(3, 4, "b"),
};

main()
{
    for (int i = 0; i < sizeof(list)/sizeof(*list); i++) {
        cout << i << " " << list[i].get_c() << endl;
    }
}

I just don’t see what information is not available for the compiler to completely instantiate these objects at compile time, so I assume I am missing something.

  • 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-11T10:42:28+00:00Added an answer on June 11, 2026 at 10:42 am

    In C++ 2011 you can create objects at compile time. For this to happen, you need to make various things constant expressions, however:

    1. The constructor needs to be declared constexpr.
    2. The entity you declare needs to be declared constexpr.

    Note, that nearly all your const qualifiers are either irrelevant or in the wrong location. Here is an example with the various correction and also actually demonstrating that the list array is initialized during compile time (by using members of it to define the values of an enum):

    #include <iostream>
    #include <iterator>
    
    class Info {
        int a;
        int b;
        char const*c;
    
    public:
        constexpr Info(int, int, char const*);
        constexpr int get_a() const { return a; }
        constexpr int get_b() const { return b; }
        constexpr char const*get_c() const { return c; }
    };
    
    constexpr Info::Info(int a, int b, char const*c)
      : a(a), b(b), c(c) {}
    
    constexpr Info list[] = {
        Info(1, 2, "a"),
        Info(3, 4, "b"),
    };
    
    enum {
        b0 = list[0].get_b(),
        b1 = list[1].get_b()
    };
    
    int main()
    {
        std::cout << "b0=" << b0 << " b1=" << b1 << "\n";
        for (Info const* it(list), *end(list); it != end; ++it) {
            std::cout << (it - list) << " " << it->get_c() << "\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code that has a lot of substitution. There's a list<char> productions
I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE:
I'm writing a very simple CRUD app that takes user stories and stores them
I've been trying to write some very fast Java code that has to do
I'm writing some code that will take a screenshot of another application, given its'
I am writing some code that will go through a file, edit it as
I am writing some code that connects to a website, and using C#, and
I am writing some code that needs to be backwards compatible with EXISTING remoting
I'm writing some code that takes a report from the mainframe and converts it
I'm writing some code that uses some unmanaged calls into user32 functions such as

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.