multidimension arrays with unknown bounds

Discussion in 'C++' started by Andy Gibbs, May 18, 2011.

  1. Andy Gibbs

    Andy Gibbs Guest

    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
     
    Andy Gibbs, May 18, 2011
    #1
    1. Advertising

  2. Andy Gibbs

    Balog Pal Guest

    "Andy Gibbs" <>
    > 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
     
    Balog Pal, May 18, 2011
    #2
    1. Advertising

  3. Andy Gibbs

    Andy Gibbs Guest

    On Wednesday, May 18, 2011 12:52 PM, Balog Pal wrote:

    >> 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
     
    Andy Gibbs, May 18, 2011
    #3
  4. Andy Gibbs

    Larry Evans Guest

    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
     
    Larry Evans, May 20, 2011
    #4
  5. Andy Gibbs

    Larry Evans Guest

    On 05/20/11 05:12, Larry Evans wrote:
    > 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
    >
    >

    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
     
    Larry Evans, May 20, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Faizan Ahmed

    Bind multidimension Array to Datagrid

    Faizan Ahmed, Oct 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    538
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=
    Oct 25, 2004
  2. Casey Hawthorne
    Replies:
    16
    Views:
    1,207
  3. Ronin
    Replies:
    4
    Views:
    395
    Ronin
    Jan 5, 2007
  4. foker
    Replies:
    29
    Views:
    798
    foker
    Sep 9, 2006
  5. Dark Ambient
    Replies:
    10
    Views:
    200
    Dark Ambient
    Aug 13, 2006
Loading...

Share This Page