Initialization of Sequences and Containers

Macro: If enabled, CXXOMFORT_USING_I12N is defined.

C++11 adds brace-initialization of lists for any and all objects (eg.: "uniform initialization").

Of particular interest is the initialization of sequences and containers, as this allows for a syntax alike to that of array initializations. This is achieved in C++11 by providing a constructor taking a std::initializer_list (const&) as argument. The end result being that advanced container types's instances can be built like this:

std::vector<int> mylist = {2009, 2010, 2011, 2012, 2013};

While brace-initialization can not itself be emulated in C++03 (because there brace-expressions do not have types), at least the syntax for container initialization can be emulated nearly fully for any STL-like container type.

Cxxomfort's sequences initialization feature provides special macros (a prolog and epilog, and a setup macro) that help initializing the sequence type by surrounding the body of code of a brace-list with accessory code creating a C-array for it then constructing the desired container from that.

The performance overhead is pretty much minimal for anything past overly complex types, as copy-construction is still required. The resulting syntax is clear to a point of near-nativeness, does in fact evaluate to native initializer-list syntax in C++11, and it does not have the ~10 MB of dependencies that Boost.Assign has.

The macro CXXO_I12N_BEGIN(containertype,name) creates an accessory container for containertype::value_type objects; then follows the brace-list that will be held in it; then the macro CXXO_I12N_END(containertype,name) creates the correct variable via iterator-range constructor and names it name.

For compilers that support variadic macros in C++03 mode, the macro CXXO_I12N_SEQ(containertype,name,{sequence}) merges the above operations in a singular expresion.

Sample code (it presumes using namespace std is in effect:


CXXO_I12N_BEG (deque<unsigned>, my_primes) = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
CXXO_I12N_END (deque<unsigned>, my_primes);
// At this point, my_primes is a container of type deque<unsigned>.

// If the compiler supports variadic macros in C++03, this can also be used:
CXXO_I12N_SEQ(vector<string>, last_names, { "Kent", "Wayne", "Jordan", "West" });

In C++11 mode or later, this form of initialization evaluates to containertype name, thus generating native C++11 code, for zero overhead.

In C++03 mode, this feature makes use of the container's iterator-range constructor, so it only takes overhead in the sense that the container's values will need to be constructed by copy.

Requirements:

  • Containers using this utility must provide an adequate value_type member type.
  • Containers using this utility must support iterator-range constructors.
  • In C++03 mode, data types using this utility must be copyable.

Known Limitations:

  • Can not be used with std::valarray in C++03 as it lacks iterator range constructor (a solution is being worked out though). Solved in cxxomfort 0.49