{1,2,3} as argument?

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

    {1,2,3} as argument?

    Hi,


    can I somehow do something like this:

    void foo(int*)
    {}

    int main()
    {
    foo( {1,2,3,4} );
    return 0;
    }



    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}


  • Kai-Uwe Bux

    #2
    Re: {1,2,3} as argument?

    Gernot Frisch wrote:
    can I somehow do something like this:
    >
    void foo(int*)
    {}
    >
    int main()
    {
    foo( {1,2,3,4} );
    return 0;
    }
    My compiler says "no"; and, I think, my compiler is right: no, you cannot.


    Best

    Kai-Uwe Bux

    Comment

    • Gernot Frisch

      #3
      Re: {1,2,3} as argument?

      >can I somehow do something like this:
      >>
      >void foo(int*)
      >{}
      >>
      >int main()
      >{
      > foo( {1,2,3,4} );
      >return 0;
      >}
      >
      My compiler says "no"; and, I think, my compiler is right: no, you
      cannot.
      Not his way, but maybe with a template or something?


      Comment

      • terminator

        #4
        Re: {1,2,3} as argument?



        On Nov 23, 12:52 pm, "Gernot Frisch" <M...@Privacy.n etwrote:
        can I somehow do something like this:
        >
        void foo(int*)
        {}
        >
        int main()
        {
        foo( {1,2,3,4} );
        return 0;
        }
        >
        My compiler says "no"; and, I think, my compiler is right: no, you
        cannot.Not his way, but maybe with a template or something?
        because of C++`s schizofernia you can not but I do not see why while
        you can use string literals which are built-in char arrays.

        Comment

        • Salt_Peter

          #5
          Re: {1,2,3} as argument?


          Gernot Frisch wrote:
          Hi,
          >
          >
          can I somehow do something like this:
          >
          void foo(int*)
          {}
          >
          int main()
          {
          foo( {1,2,3,4} );
          return 0;
          }
          >
          No, you can't.
          Passing arrays around is more complicated than passing dynamic
          containers.

          #include <iostream>

          template< typename T, const size_t Size >
          void foo( T (&array)[Size] )
          {
          for(size_t i = 0; i < Size; ++i)
          {
          std::cout << array[i] << std::endl;
          }
          }

          int main()
          {
          int arr[] = {0,1,2,3};
          foo(arr);
          }

          Note: no pointers (although technically arr decays to one).

          Comment

          • benben

            #6
            Re: {1,2,3} as argument?

            Gernot Frisch wrote:
            Hi,
            >
            >
            can I somehow do something like this:
            >
            void foo(int*)
            {}
            >
            int main()
            {
            foo( {1,2,3,4} );
            return 0;
            }
            >
            Not in the current standard. You will have to do some workarounds other
            people have suggested. Seems to me it is very likely that C++0x will get
            this right finally, but that's like, what, 5 more years from now?

            You can overload operators like << and , (comma) to get something
            similar. But for many cases this seems like an overkill and as for me
            I'll just stick to

            int temp[] = {1,2,3,4};f
            foo(temp);

            Regards,
            Ben

            Comment

            • tact

              #7
              Re: {1,2,3} as argument? You can do like this

              Gernot Frisch дµÀ:
              Hi,
              >
              >
              can I somehow do something like this:
              >
              void foo(int*)
              {}
              >
              int main()
              {
              foo( {1,2,3,4} );
              return 0;
              }
              >
              >
              >
              foo(new int[]{1,2,3,4});

              Comment

              • Gernot Frisch

                #8
                Re: {1,2,3} as argument? You can do like this

                foo(new int[]{1,2,3,4});
                Alrighty... Then I need to take care of deleting the pointer, though.


                Comment

                • Daniel T.

                  #9
                  Re: {1,2,3} as argument?

                  benben <benhonghatgmai ldotcom@nospamw rote:
                  Gernot Frisch wrote:
                  Hi,


                  can I somehow do something like this:

                  void foo(int*)
                  {}

                  int main()
                  {
                  foo( {1,2,3,4} );
                  return 0;
                  }
                  >
                  Not in the current standard. You will have to do some workarounds other
                  people have suggested. Seems to me it is very likely that C++0x will get
                  this right finally, but that's like, what, 5 more years from now?
                  I hope C++0x won't allow the above. Passing a constant array to a
                  pointer to non-const would be horrible.

                  --
                  To send me email, put "sheltie" in the subject.

                  Comment

                  • John Carson

                    #10
                    Re: {1,2,3} as argument? You can do like this

                    "Gernot Frisch" <Me@Privacy.net wrote in message
                    news:4slo92F102 qb8U1@mid.indiv idual.net
                    >foo(new int[]{1,2,3,4});
                    >
                    Alrighty... Then I need to take care of deleting the pointer, though.
                    First you will need to get it to compile. Good luck with that.

                    --
                    John Carson


                    Comment

                    • Salt_Peter

                      #11
                      Re: {1,2,3} as argument? You can do like this


                      Gernot Frisch wrote:
                      foo(new int[]{1,2,3,4});
                      >
                      Alrighty... Then I need to take care of deleting the pointer, though.
                      That sounds like a non portable extension. Fails on g++.
                      If you don't like the one given previously, try the following but makes
                      sure you make the pointer constant and do not delete, use delete[].

                      template< typename T >
                      void foo(T* const p)
                      {
                      delete [] p;
                      }

                      int main()
                      {
                      foo(new int[4]);
                      }

                      Comment

                      • Harald van Dijk

                        #12
                        Re: {1,2,3} as argument?

                        Daniel T. wrote:
                        benben <benhonghatgmai ldotcom@nospamw rote:
                        >
                        Gernot Frisch wrote:
                        Hi,
                        >
                        >
                        can I somehow do something like this:
                        >
                        void foo(int*)
                        {}
                        >
                        int main()
                        {
                        foo( {1,2,3,4} );
                        return 0;
                        }
                        >
                        Not in the current standard. You will have to do some workarounds other
                        people have suggested. Seems to me it is very likely that C++0x will get
                        this right finally, but that's like, what, 5 more years from now?
                        >
                        I hope C++0x won't allow the above. Passing a constant array to a
                        pointer to non-const would be horrible.
                        If C++0x would allow it in C99's form, it would look like

                        void foo(int*)
                        {}

                        int main()
                        {
                        foo( (int []) {1,2,3,4} );
                        return 0;
                        }

                        which behaves exactly like

                        void foo(int*)
                        {}

                        int main()
                        {
                        int dummy[] = {1,2,3,4};
                        foo(dummy);
                        return 0;
                        }

                        There's no problem with const. You're allowed to modify the data, and
                        if you don't want to, you have the possibility of writing
                        (const int []) {1,2,3,4}.

                        Comment

                        • Frederick Gotham

                          #13
                          Re: {1,2,3} as argument?

                          Gernot Frisch:
                          can I somehow do something like this:
                          >
                          void foo(int*)
                          {}
                          >
                          int main()
                          {
                          foo( {1,2,3,4} );
                          return 0;
                          }

                          Compound literals are non-standard, but are provided by many compilers.
                          Here's a taste:

                          void Func(int const (&arr)[6]) {}

                          int main()
                          {
                          Foo( (int[6]){1,2,3,4,5,6} );
                          }

                          --

                          Frederick Gotham

                          Comment

                          • LR

                            #14
                            Re: {1,2,3} as argument?

                            Gernot Frisch wrote:
                            Hi,
                            >
                            >
                            can I somehow do something like this:
                            >
                            void foo(int*)
                            {}
                            >
                            int main()
                            {
                            foo( {1,2,3,4} );
                            return 0;
                            }

                            Can you tell us a little bit about the limits of this problem?

                            Does foo have to be
                            void foo(int*)
                            or could it be
                            void foo(std::vector <int&)


                            Does the call to foo have to be
                            foo( {1,2,3} );
                            or could it look like
                            foo( V().a(1).a(2).a (3).v() );

                            LR

                            Comment

                            • Noah Roberts

                              #15
                              Re: {1,2,3} as argument?


                              Daniel T. wrote:
                              I hope C++0x won't allow the above. Passing a constant array to a
                              pointer to non-const would be horrible.
                              Well, you can do it with char* so may as well be consistant, right?

                              Comment

                              Working...