1

I need to build an array of heterogeneous types in c++. The array is constructed at compile time, however, its construction is dispersed across different compilation unit (i.e. different source files). The array should be easily extended to contain new types as the programming of the application progress. The resulting array should be accessed at run time.

Is that possible?

I delved a little bit into boost mpl and boost fusion but did not find an answer.

thanks

Ezra
  • 1,401
  • 5
  • 15
  • 33
  • If you're already using Boost, why not `std::vector`? – kennytm Jul 15 '12 at 06:57
  • 1
    To put different types in an array if they have a common base class, use unique_ptr to the base class. Otherwise, putting them in an array would make no sense. You would want a struct or tuple. – Mooing Duck Jul 15 '12 at 07:00

1 Answers1

3

The array is constructed at compile time, however, its construction is dispersed across different compilation unit

This is not possible. A translation unit doesn't know about other translation units. Since there's nothing out there except TUs, it is not possible to coordinate anything.

Suppose you want to populate the array in foo.cpp, adding an object of type Foo. At which index should it be placed? It is impossible to determine because it is unknown which other indices are there.

The linker knows about all the TUs. Alas, linkers are still rather dumb and cannot run user code at link time. If the linker were a bit smarter, it could do something like myarray.push_back(Foo()) for your TU. But it cannot.

You still can do the push_back at runtime of course.

Perhaps if you describe your real problem, the collective would be able to find a solution.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243