On 05/18/11 04:54, Andy Gibbs wrote:
Hi,
I am using gcc 4.5.3, which provides the error "multidimensional array
must have bounds for all dimensions except the first" if I try to define
an array type like...
typedef int array_t[][];
void test(array_t& arg);
[snip]
My question is this: have I simply managed to temporarily confuse the
compiler, or is there some (maybe obscure) part of the language that
allows multidimensional arrays with unknown bounds in some way?
[snip]
Hi Andy,
I think this thread:
http://thread.gmane.org/gmane.comp.lib.boost.devel/217742
may be talking about what you want. In addition to Pierre-Andre's
implementation, there's an alternative implementation mentioned
in another post in that thread:
http://article.gmane.org/gmane.comp.lib.boost.devel/218623
In that alternative implementation, the array_dyn template just
has one template parameter; hence, in your example, the
function prototype would be:
void test(array_dyn<int>& arg);
The actual sizes of each dimension is determined at run-time
by arguments to the templated CTOR:
template<typename T>
template<typename... Size>
struct array_dyn{
...
array_dyn(Size... sizes);
...
};
The downside of the array_dyn implmentation is that accessing
the elements is done with the expression:
arg(i1,i2,...,iN)
where N is the sizeof...(Size), where Size is the template
argument to the CTOR, and that expression requires N
multiplications and N-1 additions.
However, the implementation could be modified to alleviate
this problem using some of the methods described in the
Budd reference:
http://web.engr.oregonstate.edu/~budd/Books/aplc/
that was mentioned elsewhere in that thread.
Another downside is the use of variadic templates. Of course
it would be easy to simply substitute:
std::vector<unsigned>
for:
Size...
if you don't have a variadic template compiler.
HTH.
-regards,
Larry
like boost::multi_array. Like the above array_dyn, it allows
http://www.boost.org/doc/libs/1_46_1/libs/multi_array/doc/reference.html#multi_array
The main difference w.r.t. array_dyn is the dimensionality
is specified at run-time by the number of CTOR args.