{1,2,3} as argument?

Discussion in 'C++' started by Gernot Frisch, Nov 23, 2006.

  1. 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%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
     
    Gernot Frisch, Nov 23, 2006
    #1
    1. Advertising

  2. Gernot Frisch

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Nov 23, 2006
    #2
    1. Advertising


  3. >> 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?
     
    Gernot Frisch, Nov 23, 2006
    #3
  4. Gernot Frisch

    terminator Guest

    On Nov 23, 12:52 pm, "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.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.
     
    terminator, Nov 23, 2006
    #4
  5. Gernot Frisch

    Salt_Peter Guest

    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 << std::endl;
    }
    }

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

    Note: no pointers (although technically arr decays to one).
     
    Salt_Peter, Nov 23, 2006
    #5
  6. Gernot Frisch

    benben Guest

    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
     
    benben, Nov 23, 2006
    #6
  7. Gernot Frisch

    tact Guest

    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});
     
    tact, Nov 23, 2006
    #7
  8. Gernot Frisch

    Daniel T. Guest

    benben <benhonghatgmaildotcom@nospam> wrote:

    > 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.
     
    Daniel T., Nov 23, 2006
    #8
  9. 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.
     
    Gernot Frisch, Nov 23, 2006
    #9
  10. Gernot Frisch

    John Carson Guest

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

    "Gernot Frisch" <> wrote in message
    news:
    >> 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
     
    John Carson, Nov 23, 2006
    #10
  11. Gernot Frisch

    Salt_Peter Guest

    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]);
    }
     
    Salt_Peter, Nov 23, 2006
    #11
  12. Daniel T. wrote:
    > benben <benhonghatgmaildotcom@nospam> wrote:
    >
    > > 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}.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 23, 2006
    #12
  13. 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
     
    Frederick Gotham, Nov 24, 2006
    #13
  14. Gernot Frisch

    LR Guest

    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
     
    LR, Nov 24, 2006
    #14
  15. Gernot Frisch

    Noah Roberts Guest

    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?
     
    Noah Roberts, Nov 24, 2006
    #15
  16. Gernot Frisch

    Steve Pope Guest

    Frederick Gotham <> wrote:

    >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} );
    >}


    gcc barfs on this, as well it should.

    Steve
     
    Steve Pope, Nov 24, 2006
    #16
  17. Steve Pope:

    >>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} );
    >>}

    >
    > gcc barfs on this, as well it should.



    Sorry, there must be some confusion -- this is comp.lang.c++, not
    comp.lang.c++.my.implementation.which.has.an.extension.known.as.compound.lite
    rals.but.which.wont.let.me.pass.an.array.compound.literal.to.a.function.which
    ..takes.a.reference.to.a.const.array

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 24, 2006
    #17
  18. Gernot Frisch

    Ian Collins Guest

    Frederick Gotham wrote:
    > Steve Pope:
    >
    >
    >>>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} );
    >>>}

    >>
    >>gcc barfs on this, as well it should.

    >
    > Sorry, there must be some confusion -- this is comp.lang.c++, not
    > comp.lang.c++.my.implementation.which.has.an.extension.known.as.compound.lite
    > rals.but.which.wont.let.me.pass.an.array.compound.literal.to.a.function.which
    > ..takes.a.reference.to.a.const.array
    >

    What? You said "Compound literals are non-standard" so the compiler
    should barf. Or am I missing something?

    --
    Ian Collins.
     
    Ian Collins, Nov 24, 2006
    #18
  19. Ian Collins:

    > What? You said "Compound literals are non-standard" so the compiler
    > should barf. Or am I missing something?



    My lack of complaint perhaps?

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 24, 2006
    #19
  20. Gernot Frisch

    terminator Guest

    benben wrote:
    > 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


    I insist that C++ is paranoic;I cant see why one shoud be able to
    write:
    f("123");
    but not:
    f({'1','2','3','\0'});
    this is painful

    reguards
    FM
     
    terminator, Nov 28, 2006
    #20
    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. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    416
  2. Ben Kial
    Replies:
    1
    Views:
    659
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,892
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    314
  5. Reckoner
    Replies:
    11
    Views:
    703
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page