Using templates with arrays

Discussion in 'C++' started by Zara, Sep 25, 2006.

  1. Zara

    Zara Guest

    I have been trying to substitute the macro that comes with the
    compiler with the following template:

    template <typename ITEM,size_t SIZE>
    inline size_t arraySize(ITEM const (&array)[SIZE]) {
    return SIZE;
    }

    AFAIK, it is compliant with the standard, and it should work.

    In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    3.4.1.

    Should it work, from the Standard point of view, or am i missing
    something?

    Best regards,

    Zara
    Zara, Sep 25, 2006
    #1
    1. Advertising

  2. Zara

    Guest

    Zara wrote:
    > I have been trying to substitute the macro that comes with the
    > compiler with the following template:
    >
    > template <typename ITEM,size_t SIZE>
    > inline size_t arraySize(ITEM const (&array)[SIZE]) {
    > return SIZE;
    > }
    >
    > AFAIK, it is compliant with the standard, and it should work.
    >
    > In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    > 3.4.1.


    The code is fine and it is a very common teqnique used for getting size
    of an array.

    >
    > Should it work, from the Standard point of view, or am i missing
    > something?
    >
    > Best regards,
    >
    > Zara
    , Sep 25, 2006
    #2
    1. Advertising

  3. Zara

    Zara Guest

    On Mon, 25 Sep 2006 06:06:52 +0200, Zara <>
    wrote:

    >I have been trying to substitute the macro that comes with the
    >compiler with the following template:
    >
    >template <typename ITEM,size_t SIZE>
    > inline size_t arraySize(ITEM const (&array)[SIZE]) {
    > return SIZE;
    > }

    <..>
    >In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    >3.4.1.
    >


    So sorry for this comment, it works on both. The problem comes from
    using an empty intilizer list for the array:

    int data[]={};

    This compiles on GCC and not on BCB, but after reading the relevant
    part of C++Std, it should not compile (8.5.1-4 and footnote 91). My
    fault.

    Regrads,

    Zara
    Zara, Sep 25, 2006
    #3
  4. Zara

    Rolf Magnus Guest

    Zara wrote:

    >>In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    >>3.4.1.
    >>

    >
    > So sorry for this comment, it works on both. The problem comes from
    > using an empty intilizer list for the array:
    >
    > int data[]={};
    >
    > This compiles on GCC and not on BCB, but after reading the relevant
    > part of C++Std, it should not compile (8.5.1-4 and footnote 91).


    My guess is that you forgot to turn on all the warning options for gcc.
    Rolf Magnus, Sep 25, 2006
    #4
  5. Zara

    Zara Guest

    On Mon, 25 Sep 2006 07:32:26 +0200, Rolf Magnus <>
    wrote:

    >Zara wrote:
    >
    >>>In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    >>>3.4.1.
    >>>

    >>
    >> So sorry for this comment, it works on both. The problem comes from
    >> using an empty intilizer list for the array:
    >>
    >> int data[]={};
    >>
    >> This compiles on GCC and not on BCB, but after reading the relevant
    >> part of C++Std, it should not compile (8.5.1-4 and footnote 91).

    >
    >My guess is that you forgot to turn on all the warning options for gcc.


    [OT]
    I do use:

    -pedantic -pedantic-errors -Wall -Werror -std=c++98

    It seems ther is no diagnostic for this error.

    [OT]
    Zara, Sep 25, 2006
    #5
  6. Zara

    Rolf Magnus Guest

    Zara wrote:

    >>> So sorry for this comment, it works on both. The problem comes from
    >>> using an empty intilizer list for the array:
    >>>
    >>> int data[]={};
    >>>
    >>> This compiles on GCC and not on BCB, but after reading the relevant
    >>> part of C++Std, it should not compile (8.5.1-4 and footnote 91).

    >>
    >>My guess is that you forgot to turn on all the warning options for gcc.

    >
    > [OT]
    > I do use:
    >
    > -pedantic -pedantic-errors -Wall -Werror -std=c++98
    >
    > It seems ther is no diagnostic for this error.


    Then it's the version. I'm using 4.1.2, and with -pedantic, it says:

    error: zero-size array ‘data’

    > [OT]
    Rolf Magnus, Sep 25, 2006
    #6
  7. Zara

    Marcus Kwok Guest

    Zara <> wrote:
    > I have been trying to substitute the macro that comes with the
    > compiler with the following template:
    >
    > template <typename ITEM,size_t SIZE>
    > inline size_t arraySize(ITEM const (&array)[SIZE]) {
    > return SIZE;
    > }
    >
    > AFAIK, it is compliant with the standard, and it should work.
    >
    > In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
    > 3.4.1.
    >
    > Should it work, from the Standard point of view, or am i missing
    > something?


    I see you found that your problem was with a size-0 array, but this
    article by Alf P. Steinbach has a couple other approaches, along with
    their pro's and con's:

    http://home.no.net/dubjai/win32cpptut/special/pointers/array_size.doc.pdf

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Sep 25, 2006
    #7
    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. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  2. JKop
    Replies:
    3
    Views:
    468
  3. recover
    Replies:
    2
    Views:
    800
    recover
    Jul 25, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  5. shashi
    Replies:
    17
    Views:
    414
    Guest
    Apr 13, 2006
Loading...

Share This Page