tr1::array initializater syntax

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

    tr1::array initializater syntax

    The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
    with the traditional syntax:

    #include <iostream>
    #include <tr1/array>

    int main() {
    std::tr1::array <int, 35a = { 0 };
    std::cout << a[4] << '\n';
    }

    g++ -ansi -pedantic -Wall main.cc -o main
    main.cc: In function 'int main()':
    main.cc:5: warning: missing braces around initializer for 'int [35]'

    Is this the expected behavior? What's the short, static way to
    initialize a tr1::array of arbitrary size?
  • Victor Bazarov

    #2
    Re: tr1::array initializater syntax

    Jeff Schwab wrote:
    The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
    with the traditional syntax:
    >
    #include <iostream>
    #include <tr1/array>
    >
    int main() {
    std::tr1::array <int, 35a = { 0 };
    std::cout << a[4] << '\n';
    }
    >
    g++ -ansi -pedantic -Wall main.cc -o main
    main.cc: In function 'int main()':
    main.cc:5: warning: missing braces around initializer for 'int
    [35]'
    Is this the expected behavior? What's the short, static way to
    initialize a tr1::array of arbitrary size?
    Since 'std::tr1::arra y' has the actual array as the 'elems' member,
    you need to take your zero in curly braces:

    std::tr1::array <int, 35a = { {0} };

    That's what the compiler is telling you, BTW.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Pete Becker

      #3
      Re: tr1::array initializater syntax

      On 2008-02-14 16:00:20 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
      The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
      with the traditional syntax:
      >
      #include <iostream>
      #include <tr1/array>
      >
      int main() {
      std::tr1::array <int, 35a = { 0 };
      std::cout << a[4] << '\n';
      }
      >
      g++ -ansi -pedantic -Wall main.cc -o main
      main.cc: In function 'int main()':
      main.cc:5: warning: missing braces around initializer for 'int [35]'
      >
      Is this the expected behavior? What's the short, static way to
      initialize a tr1::array of arbitrary size?
      The compiler is telling you that you might not know what you're doing,
      despite the fact that this initialization is legal and its meaning is
      well defined. If you agree with the compiler writer's stylistic
      judgment, change the code. Personally, I don't think any compiler
      writer knows enough about the code that I'm writing to tell me how I
      should write it. Turn off the warning.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • Pete Becker

        #4
        Re: tr1::array initializater syntax

        On 2008-02-14 16:16:27 -0500, "Victor Bazarov" <v.Abazarov@com Acast.netsaid:
        Jeff Schwab wrote:
        >The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
        >with the traditional syntax:
        >>
        >#include <iostream>
        >#include <tr1/array>
        >>
        >int main() {
        >std::tr1::arra y<int, 35a = { 0 };
        >std::cout << a[4] << '\n';
        >}
        >>
        >g++ -ansi -pedantic -Wall main.cc -o main
        >main.cc: In function 'int main()':
        >main.cc:5: warning: missing braces around initializer for 'int
        >[35]'
        >Is this the expected behavior? What's the short, static way to
        >initialize a tr1::array of arbitrary size?
        >
        Since 'std::tr1::arra y' has the actual array as the 'elems' member,
        you need to take your zero in curly braces:
        >
        std::tr1::array <int, 35a = { {0} };
        >
        That's what the compiler is telling you, BTW.
        >
        Well, it's not saying that you "need to" put in the unnecessary braces.
        All it's doing is whining about some compiler writer's notion of good
        style.

        --
        Pete
        Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
        Standard C++ Library Extensions: a Tutorial and Reference
        (www.petebecker.com/tr1book)

        Comment

        • Jeff Schwab

          #5
          Re: tr1::array initializater syntax

          Pete Becker wrote:
          On 2008-02-14 16:16:27 -0500, "Victor Bazarov" <v.Abazarov@com Acast.net>
          said:
          >
          >Jeff Schwab wrote:
          >>The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
          >>with the traditional syntax:
          >>>
          >>#include <iostream>
          >>#include <tr1/array>
          >>>
          >>int main() {
          >>std::tr1::arr ay<int, 35a = { 0 };
          >>std::cout << a[4] << '\n';
          >>}
          >>>
          >>g++ -ansi -pedantic -Wall main.cc -o main
          >>main.cc: In function 'int main()':
          >>main.cc:5: warning: missing braces around initializer for 'int
          >>[35]'
          >>Is this the expected behavior? What's the short, static way to
          >>initialize a tr1::array of arbitrary size?
          >>
          >Since 'std::tr1::arra y' has the actual array as the 'elems' member,
          >you need to take your zero in curly braces:
          >>
          > std::tr1::array <int, 35a = { {0} };
          >>
          >That's what the compiler is telling you, BTW.
          Thank you. I should have looked more carefully before posting.
          Well, it's not saying that you "need to" put in the unnecessary braces.
          All it's doing is whining about some compiler writer's notion of good
          style.
          Then standard C++ does *not* require the double braces? If not, I'd
          rather not use them.

          What surprises me is that I had vaguely expected to be able to change
          array types by changing a typedef in a library header. If I don't want
          clients to see warnings, though, it looks like any such initializations
          have to be changed in the client code:

          #include <iostream>
          #include <tr1/array>

          struct my_lib {

          /* Use tr1::array as a drop-in replacement for raw array. */
          // typedef int array_type[32];
          typedef std::tr1::array <int, 32array_type;
          };

          int main() {

          /* Oops! Brace-related warning from some compilers. */
          my_lib::array_t ype an_array = { 0 };

          (void)an_array;
          }

          This doesn't actually bite me too hard, since I rarely expose raw arrays
          like that anyway, but I imagine some folks will have a problem with it.
          It sounds like this is a QoI issue, though, not a language or library
          issue; is that correct?

          Comment

          • Pete Becker

            #6
            Re: tr1::array initializater syntax

            On 2008-02-14 20:37:32 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
            >
            Then standard C++ does *not* require the double braces? If not, I'd
            rather not use them.
            >
            Aggregate initialization just goes element by element. You can also
            have parentheses around the initializer for an element, but it's not
            required.

            Here's a more elaborate example:

            struct S
            {
            int i, j;
            };

            In all the following array definitions, pretend that the definition
            occurs inside a function.

            S s[2] = { 1, 2, 3 };

            This initializes s[0].i to 1, s[0].j to 2, s[1].i to 3, and s[1].j to
            0. The fully-bracketed form (which is probably preferable here) would
            be:

            S s[2] = { { 1, 2 }, { 3, 0 } };

            On the other hand, all-zeros initialization is far more obvious, so I
            usually go with this:

            S s[2] = { 0 };

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            • Pete Becker

              #7
              Re: tr1::array initializater syntax

              On 2008-02-14 21:46:37 -0500, Pete Becker <pete@versatile coding.comsaid:
              On 2008-02-14 20:37:32 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
              >
              >>
              >Then standard C++ does *not* require the double braces? If not, I'd
              >rather not use them.
              >>
              >
              Aggregate initialization just goes element by element. You can also
              have parentheses around the initializer for an element, but it's not
              required.
              >
              Here's a more elaborate example:
              >
              struct S
              {
              int i, j;
              };
              >
              And here's a wierd one I forgot:

              S s[2] = { {1}, 2, 3 };

              This initializes s[0].i to 1, s[0].j to 0, s[1].i to 2, and s[1].j to 3.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • Jeff Schwab

                #8
                Re: tr1::array initializater syntax

                Pete Becker wrote:
                On 2008-02-14 21:46:37 -0500, Pete Becker <pete@versatile coding.comsaid:
                >
                >On 2008-02-14 20:37:32 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
                >>
                >>>
                >>Then standard C++ does *not* require the double braces? If not, I'd
                >>rather not use them.
                >>>
                >>
                >Aggregate initialization just goes element by element. You can also
                >have parentheses around the initializer for an element, but it's not
                >required.
                >>
                >Here's a more elaborate example:
                >>
                >struct S
                >{
                >int i, j;
                >};
                >>
                >
                And here's a wierd one I forgot:
                >
                S s[2] = { {1}, 2, 3 };
                >
                This initializes s[0].i to 1, s[0].j to 0, s[1].i to 2, and s[1].j to 3.
                That is fascinating. I'm always amazed at how much I don't know about
                this language. Thanks for the explanation.

                Comment

                Working...