Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

{1,2,3} as argument?

 
 
Salt_Peter
Guest
Posts: n/a
 
      11-23-2006

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

 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      11-23-2006
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}.

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      11-24-2006
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
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      11-24-2006
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
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      11-24-2006

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?

 
Reply With Quote
 
Steve Pope
Guest
Posts: n/a
 
      11-24-2006
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
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-24-2006
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.exten sion.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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-24-2006
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.exten sion.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.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-24-2006
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
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-28-2006

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

 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57