Are Anonymous arrays possible

  • Thread starter Ramprasad A Padmanabhan
  • Start date
R

Ramprasad A Padmanabhan

Suppose I have a function that expects an array as argument
{
.....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

{
.....
someFunc({1,2,3,4});
......
}


Thanks
Ram
 
A

Alf P. Steinbach

* Ramprasad A Padmanabhan:
Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

{
....
someFunc({1,2,3,4});
.....
}

Well, there are three issues:

* Avoiding an _automatic_ (local variable) array.

* Specifying the values of a conceptual array in a function call.

* Efficiency of such a specification.

Regarding how to avoid a local variable, initialized each time, if
concurrency is not an issue you can declare the array static:

static int const d[] = {1, 2, 3, 4};
somefunc( d );

Regarding how to specify the values of a conceptual array in a function
call C++ doesn't have a notation for that, so it reduces to the general
problem of specifying an arbitrary number of arguments. The usual
solution, e.g. employed by std::cout "<<", is to use a member operator
or function that returns a reference to the object it's called on. Like
(disregarding issues related to deriving from a class with no virtual
constructor -- you might want to use encapsulation instead)

class Array: public std::vector<int>
{
public:
Array& operator<<( int x ){ push_back( x ); return *this; }
};

inline Array const& temporary( Array const& a ){ return a; }

...
d( temporary( Array() << 1 << 2 << 3 << 4 ) );

Possibly there is such a thing in Boost, <url: http://www.boost.org>, I
think I saw something like that.

Regarding efficiency, first make correct, then check if good enough,
then if it isn't, measure (unless obvious), and only then optimize. :)
 
R

Ron Natalie

Ramprasad said:
Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

The thing that doesn't exist that you are asking for is an "array literal"
which other than for the case of char strings, don't exist in the language.
 
M

marbac

Ramprasad said:
Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

There is no temporary array. There is only one array (d).
If you call type someFunc(d); then the functionheader has to have a
pointer to the array d ( type someFunc(int *myarray) ). Example:


#include <iostream>
using namespace std;


void add_one (int *anarray, int size) { //increments each element
for (int tmp=0; tmp<size; tmp++) anarray[tmp]++;
}

int main () {

int test [4] = {33,22,11,55};
int elements = sizeof(test)/sizeof(test[0]); //not known within function

add_one (test,elements);

for (int tmp=0; tmp<elements; tmp++)
cout << "Element No." << tmp << " : " << test[tmp] << endl;

return 0;
}

{
....
someFunc({1,2,3,4});
.....
}


If you use something like "type someFunc({1,2,3,4});"

then this would be the same as

type someFunc () {
int d[] = {1,2,3,4};
....
}

the array is only valid within the function.the case
"someFunc({1,2,3,4})" doesnt work as far as i know.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top