473,804 Members | 3,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8901
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(ma ke_array_t const&);
};

// and here is the factory:
template<class Tmake_array_t<T make_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...@aggiem ail.usu.eduwrot e:
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.*****@aggiem ail.usu.eduwrot e in news:5b04358b-3510-
43************* *******@h11g200 0p...legrou ps.com:
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<int v;
v += 1,2,3,4,5;

And a few other collection initializing tricks.

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

std::vector<int v;
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...@doublet ake.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...@doublet ake.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
10188
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 code... TCHAR myArray; DoStuff(myArray);
8
4119
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
1738
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 the descriptor or pointer to the array to the function
4
5308
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 of struct */ #include <stdio.h>
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.