473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

passing unnamed array to function

I've been unable to find any information on this through any search
engines. Is there any way to pass an unnamed (temporary) array to a
function?
e.g.

void f0(const int ar[5]);
void f1(const int ar[]);
f0( {1, 2, 3, 4, 5} ); //does not compile
f1( {1, 2, 3} ); //does not compile

int n_set[] = {1, 2, 3, 4, 5};
f0(n_set); //these both compile (of course)
f1(n_set);

I know I can use an STL container instead and pass an unnamed STL
container, but an STL container doesn't allow me to set construct it
with, say, 5 arbitrary values.

So I guess my question is, why can I not initialize a function
argument with the array "shorthand" notation, and is there an
alternative syntax that will allow this?

thanks,
--Jason
Jan 3 '08 #1
6 8829
poorboywilly wrote:
I've been unable to find any information on this through any search
engines. Is there any way to pass an unnamed (temporary) array to a
function?
There are no "array literals" in C++ (beyond arrays of 'char const').

You can probably create a way. Here is what you need to do. Define
a class (better a template) which will have a constructor with '...'
for the argument. It will manage its own memory, say. A member of
that class will be the pointer to the dynamic memory, so you would
be able to use syntax like

f1(make_array(1,2,3,4).pointer);

It might look something like

template<class Tstruct make_array_t {
T *pointer;
make_array_t(T t ...); // the constructor - allocates and
// fills the memory
~make_array_t();
private:
// those are prohibited
make_array_t& operator=(make_array_t const&);
make_array_t(make_array_t const&);
};

// and here is the factory:
template<class Tmake_array_t<Tmake_array(T t ...);

All you need to figure out is how you manage the variadic arguments
(how you stop processing the list, that is).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '08 #2
On Jan 3, 2:08*pm, poorboywilly <j.cr...@aggiemail.usu.eduwrote:
I've been unable to find any information on this through any search
engines. *Is there any way to pass an unnamed (temporary) array to a
function?
e.g.

void f0(const int ar[5]);
void f1(const int ar[]);

f0( {1, 2, 3, 4, 5} ); * * //does not compile
f1( {1, 2, 3} ); * * * * * *//does not compile
[snip]

What are you trying to accomplish through this?
Possibly there's a better way to get it than by
doing this.
int n_set[] = {1, 2, 3, 4, 5};
f0(n_set); //these both compile (of course)
f1(n_set);
If you just put that in a scope, say with { }
around it, would that accomplish what you want?
Or by putting this stuff inside a function call.
Socks
Jan 3 '08 #3
poorboywilly <j.*****@aggiemail.usu.eduwrote in news:5b04358b-3510-
43********************@h11g2000prf.googlegroups.co m:
I've been unable to find any information on this through any search
engines. Is there any way to pass an unnamed (temporary) array to a
function?
e.g.

void f0(const int ar[5]);
void f1(const int ar[]);
f0( {1, 2, 3, 4, 5} ); //does not compile
f1( {1, 2, 3} ); //does not compile

int n_set[] = {1, 2, 3, 4, 5};
f0(n_set); //these both compile (of course)
f1(n_set);

I know I can use an STL container instead and pass an unnamed STL
container, but an STL container doesn't allow me to set construct it
with, say, 5 arbitrary values.

So I guess my question is, why can I not initialize a function
argument with the array "shorthand" notation, and is there an
alternative syntax that will allow this?
The short answer is because the C++ syntax doesn't allow it. The longer
answer is that in general, objects have to exist somewhere. In the case
of temporary objects, this is normally on the stack, though compilers
are allowed to put them anywhere. Thing is, after the temporary goes
out of scope, the memory is reclaimed. This doesn't leave a lot of
options for temporary arrays. Most of the languages which allow what
you wrote also have garbage collectors tightly integrated with the
language. This more easily allows temporaries to be put in the garbage
collected heap, because you don't have to worry about leaking the
allocated memory. This kind of tight integration with a garbage
collector will never be the case in C++.

You could look at the 'assign' library in boost. It lets you write
things like.

std::vector<intv;
v += 1,2,3,4,5;

And a few other collection initializing tricks.

HTH,
joe
Jan 3 '08 #4
Joe Greer <jg****@doubletake.comwrote in news:Xns9A1A976967FAFjgreerdoubletakecom@
194.177.96.78:
>
You could look at the 'assign' library in boost. It lets you write
things like.

std::vector<intv;
v += 1,2,3,4,5;

And a few other collection initializing tricks.
Actually, the boost assign library has a to_list() routine which allows for the
construction of temporary arrays (at least in some contexts).

joe
Jan 3 '08 #5
On Jan 3, 12:53 pm, Joe Greer <jgr...@doubletake.comwrote:
>
The short answer is because the C++ syntax doesn't allow it. The longer
answer is that in general, objects have to exist somewhere. In the case
of temporary objects, this is normally on the stack, though compilers
are allowed to put them anywhere. Thing is, after the temporary goes
out of scope, the memory is reclaimed. This doesn't leave a lot of
options for temporary arrays. Most of the languages which allow what
you wrote also have garbage collectors tightly integrated with the
language. This more easily allows temporaries to be put in the garbage
collected heap, because you don't have to worry about leaking the
allocated memory. This kind of tight integration with a garbage
collector will never be the case in C++.
I don't quite see it...the compiler knows at compile time how big this
array is going to be, and that it will last the length of the function
call. It knows how much space to reserve on the stack and then how
much to take off when it is done.

--Jason
Jan 4 '08 #6
poorboywilly wrote:
On Jan 3, 12:53 pm, Joe Greer <jgr...@doubletake.comwrote:
>The short answer is because the C++ syntax doesn't allow it. The longer
answer is that in general, objects have to exist somewhere. In the case
of temporary objects, this is normally on the stack, though compilers
are allowed to put them anywhere. Thing is, after the temporary goes
out of scope, the memory is reclaimed. This doesn't leave a lot of
options for temporary arrays. Most of the languages which allow what
you wrote also have garbage collectors tightly integrated with the
language. This more easily allows temporaries to be put in the garbage
collected heap, because you don't have to worry about leaking the
allocated memory. This kind of tight integration with a garbage
collector will never be the case in C++.

I don't quite see it...the compiler knows at compile time how big this
array is going to be, and that it will last the length of the function
call. It knows how much space to reserve on the stack and then how
much to take off when it is done.
Everything after the first sentence above is a bit of a red herring, the
most concise explanation was from Victor - 'There are no "array
literals" in C++ (beyond arrays of 'char const').'

--
Ian Collins.
Jan 4 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
7
by: =?Utf-8?B?YmVyaWNr?= | last post by:
New to this, I used to pass an array like this function BytesToString(byref myarray() as byte, somethingelse as long) as long and m = BytesToString(fooBar(), bluenose) This would send...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.