Using templates with arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zara

    #1

    Using templates with arrays

    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
  • amparikh@gmail.com

    #2
    Re: Using templates with arrays


    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

    Comment

    • Zara

      #3
      Re: Using templates with arrays

      On Mon, 25 Sep 2006 06:06:52 +0200, Zara <me_zara@dea.sp amcon.org>
      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

      Comment

      • Rolf Magnus

        #4
        Re: Using templates with arrays

        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.

        Comment

        • Zara

          #5
          Re: Using templates with arrays

          On Mon, 25 Sep 2006 07:32:26 +0200, Rolf Magnus <ramagnus@t-online.de>
          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]

          Comment

          • Rolf Magnus

            #6
            Re: Using templates with arrays

            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]

            Comment

            • Marcus Kwok

              #7
              Re: Using templates with arrays

              Zara <me_zara@dea.sp amcon.orgwrote:
              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:



              --
              Marcus Kwok
              Replace 'invalid' with 'net' to reply

              Comment

              Working...