0

TL;DR : I need global objects to be initialized at compile time, and I cannot use constexpr constructors. Can I use aggregate initializers for this task?


I know that C/C++ aggregate initializers can be used to initialize an object's public members like this :

MyObj obj = { "object1", 3, 7.2F };

I discovered their existence not long ago, and I thought about a previous issue I had.

I have a global object with a class type which I'm currently initializing using a constructor :

MyObj obj = MyObj("object1", 3, 7.2F);

This constructor only sets a few members with the given arguments. The problem is that I'd like the object's members to already be initialized inside the application's executable (like a compile-time constructor), which is not the case. Yes, I have inspected the resulting binary file, and the object's space is zeroed.

I cannot use constexpr on the constructor, because I'm working without modern C++ and have no way to upgrade.

This is why I'd like to know if I can use aggregate initializers to solve my issue.


EDIT 1 : I forgot to mention that I am developing for Windows, and that the objects I want to initialize have the __declspec(dllexport) and extern "C" specifiers.

Dave
  • 41
  • 8
  • 1
    Why do you need this? If you initialize the object globally, then by the time `main` starts, it will have the proper value. Why does it matter exactly when this happens? – Nicol Bolas Jul 20 '21 at 16:50
  • 3
    XY problem? Why do you want initialization to happen at compile time (as opposed to start up time)? – SergeyA Jul 20 '21 at 16:53
  • I must have these objects initialized because I am writing a "module" loader for extracting these objects from my program's executable. They're not only needed for the application. – Dave Jul 20 '21 at 17:13
  • 1
    which platform are you on? Why don't you have modern C++? C++11 has been there for 10 years – phuclv Jul 20 '21 at 17:25
  • I am developing for Windows (7 to 10), and I am not using modern C++ because my toolset does not support it. – Dave Jul 20 '21 at 17:33
  • Did you make the object `const`? If not it has to appear in RW memory and that means it's not likely going to be in the `text` portion of the loaded memory (where the binary gets loaded) - it's going to be in memory external to the executable. The initializer itself will have to be in the `text` portion, but it will have to be copied over. – Galik Jul 20 '21 at 17:33
  • Isn't the `text` section the *code* section? But yes, I have tried to make the object `const` and it did not change anything. – Dave Jul 20 '21 at 17:37
  • Yes, `text` is the code section. None of this has to do with `C++` itself tbh, it is down to the compiler to implement something like this as an optimization perhaps (if the Standard allows for it). – Galik Jul 20 '21 at 17:38
  • 2
    The C++ language doesn't use the terms "compile/run time", these things are meaningless as far as the language is concerned. They potentially have meaning in the context of your specific toolset. Most toolsets place initialised data in some kind of data section, ready for use, and do not generate any code to initialise such data, *if they can* (e.g. when performing [constant initialisation](https://en.cppreference.com/w/cpp/language/constant_initialization) ([demo](https://godbolt.org/z/h5czsMWGE)). But there is no guarantee of course. If your experience is different, please post a [mcve]. – n. m. could be an AI Jul 20 '21 at 20:02

1 Answers1

0

As n. 1.8e9-where's-my-share m. said, the terms "compile-time" and "run-time" are meaningless as far as the language is concerned. They are more likely to concern the toolset you're working with.

Best answer I can give here is "uncertain". The behavior of aggregate initializers is not part of the C++ standard, which means it is implementation-defined.

To answer my problem, the best bets I have are either switching to modern C++ and use constexpr constructors, or writing my data in ASM files.

Dave
  • 41
  • 8
  • Or, perhaps, *find out* what your implementation does with these initializers. (Which also depends on what your constructors look like). Switching to a more modern toolset is always good, mixing in ASM not so much -- and perhaps you don't **have** a problem to solve in the first place. (I.e. initialization of POD structures like that is basically *guaranteed* to be resolved at compile-time.) – DevSolar Jul 22 '21 at 11:26