Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > {1,2,3} as argument?

Reply
Thread Tools

{1,2,3} as argument?

 
 
Gernot Frisch
Guest
Posts: n/a
 
      11-23-2006
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);}


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-23-2006
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
 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      11-23-2006

>> 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?


 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-23-2006


On Nov 23, 12:52 pm, "Gernot Frisch" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-23-2006

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).

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      11-23-2006
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
 
Reply With Quote
 
tact
Guest
Posts: n/a
 
      11-23-2006
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});
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      11-23-2006
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.
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      11-23-2006

> foo(new int[]{1,2,3,4});


Alrighty... Then I need to take care of deleting the pointer, though.


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      11-23-2006
"Gernot Frisch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments