C++11 mapping for IDL arrays

Has to be defined

Your rating: None Average: 2 (1 vote)

Formal IDL to C++11 array mapping

The formal IDL to C++11 array mapping is available as part of the official IDL to C++11 specification.

std::array<>

I think std::array should be used as it is typesafe (does not automatically decay to a raw pointer) and keeps the array size at all times.


typedef double DoubleArray[15];

would be mapped to


typedef std::array<double, 15> DoubleArray;

Trent Nadeau
Northrop Grumman Corp.

std::array

And what for multi dimensional arrays?

Multidimensional arrays

It's not particularly elegant, but you can do the following:


// IDL
typedef double My2D_DoubleArray[5][3];


// C++
// Note that the bounds are in reverse order
typedef std::array<std::array<double, 3>, 5> My2D_DoubleArray;

If this is too ugly, another possibility is a wrapper class that has a similar interface to std::array but supports multi-dimensional types directly. This type could be templated on the type, number of dimensions, and (using variadic templates) the bound of each dimension.

The above IDL could then be mapped to something like:


// The second "number of dimensions" argument
// may be unnecessary depending on how variadic
// templates work in practice
typedef ArrayType<double, 2, 5, 3> My2D_DoubleArray;


// Use it
My2D_DoubleArray myArray;
// Set element using operator() which can take any
// number of args instead of operator[]
myArray(1,2) = 5;

The types defined by the VSIPL++ standard could be useful for an interface guide (only using a subset of course).

Trent Nadeau
Northrop Grumman Corp.

multi-dimensional std::array using template alias

Building on what Trent suggested, you could simplify creation of multi-dimensional std::array using a template alias.

template<class T, size_t ROW, size_t COL>
using Matrix = std::array<std::array<T, COL>, ROW>

Creating arbitrary dimension arrays is however not possible with this (I think). So you may use recursive type nesting like below.

template <class T, size_t I, size_t... J>
struct MultiDimArray {
  using Nested = typename MultiDimArray<T, J...>::type;
  using type = std::array<Nested, I>;
};

template <class T, size_t I>
struct MultiDimArray<T, I> {
  using type = std::array<T, I>;
};
MultiDimArray<float, 3, 4, 5, 6>::type Floats;

As of today, only clang accepts this program. If it is of any interest, I've blogged about using native arrays in the same fashion here.

example mapping to std::array

I have created an example using the mapping to std::array. The new C++11 specification doesn't has a multidimensional array type at this moment. You can find the example on OSportal