multidimension arrays with unknown bounds

A

Andy Gibbs

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);

I'm ok with this since I know C/C++ doesn't support this sort of array type.
However, it is possible to do this...

template <typename X>
struct add { typedef X type[]; };

typedef typename add<typename add<int>::type>::type array_t;

void test(array_t& arg);

.... and this *does* create a multidimensional array with unknown bounds. It
still errors on the function prototype: "error: parameter 'arg' includes
reference to array of unknown bound 'int [][]'" so while interesting it is
not much obvious use.

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?

Thanks for any light that may be shed on this matter!

Andy
 
B

Balog Pal

Andy Gibbs said:
template <typename X>
struct add { typedef X type[]; };

typedef typename add<typename add<int>::type>::type array_t;

void test(array_t& arg);

... and this *does* create a multidimensional array with unknown bounds.
It still errors on the function prototype: "error: parameter 'arg'
includes reference to array of unknown bound 'int [][]'" so while
interesting it is not much obvious use.

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?

You did confuse gcc.

Cameau (http://www.comeaucomputing.com/tryitout/) flags your typedef
attempt:

"ComeauTest.c", line 2: error: an array may not have elements of this type
struct add { typedef X type[]; };
^
detected during instantiation of class "add<X> [with X=int []]" at
line 4
 
A

Andy Gibbs

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?

You did confuse gcc.

Cameau (http://www.comeaucomputing.com/tryitout/) flags your typedef
attempt:

"ComeauTest.c", line 2: error: an array may not have elements of this type
struct add { typedef X type[]; };
^
detected during instantiation of class "add<X> [with X=int []]"
at
line 4

That's a tool worth knowing about. And it answers the question too. Thanks
very much!

Cheers
Andy
 
L

Larry Evans

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
 
L

Larry Evans

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
After some more thought, you may be looking for something
like boost::multi_array. Like the above array_dyn, it allows
specification of sizes at run-time via the sizes argument
to CTOR:

template <typename ExtentList>
explicit multi_array(const ExtentList& sizes,
const storage_order_type store
= c_storage_order(),
const Allocator& alloc = Allocator());

(See

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
of multi_array is fixed by the NumDims multi_array template
argument whereas the dimensionality of array_dyn
is specified at run-time by the number of CTOR args.

-regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top