{1,2,3} as argument?

G

Gernot Frisch

Hi,


can I somehow do something like this:

void foo(int*)
{}

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

Kai-Uwe Bux

Gernot said:
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
 
T

terminator

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

Salt_Peter

Gernot said:
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).
 
B

benben

Gernot said:
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
 
D

Daniel T.

benben said:
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.
 
S

Salt_Peter

Gernot said:
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]);
}
 
G

Guest

Daniel said:
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}.
 
F

Frederick Gotham

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

LR

Gernot said:
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
 
N

Noah Roberts

Daniel said:
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?
 
S

Steve Pope

Frederick Gotham said:
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
 
F

Frederick Gotham

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
 
I

Ian Collins

Frederick said:
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?
 
F

Frederick Gotham

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

terminator

benben said:
Gernot said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top