BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Is it possible to pass an anonymous ary to a function?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Dec 19, 2006 9:10 am    Post subject: Is it possible to pass an anonymous ary to a function? Reply with quote



I am wondering, if it is possible to pass an anonymous ary to a
function?

Having a function declaration

void fct(int[]);

the following forks fine:

int a[]={1,2,3};
fct(a);

Can I pass {1,2,3} somehow without temporarily storing it inside a
var?


E.g. I tried

fct(int[]({1,2,3}));

but this doesn't work


~~~~~~~~~~

As a following step I would like to pass the number of the
array-indices

void fct(int ary[], size_t ary_size);

The working non-anonymouse-var-version would be

#define TUPEL_ARY_SIZE(ary) ary, sizeof(ary)/sizeof(*ary)

int a[]={1,2,3};
fct(TUPEL_ARY_SIZE(a));

~~~~~~~~~~

It's just a tryout: if the result looks too ugly, I would use the
working version above...


Thanks,

Michael
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Dec 19, 2006 9:10 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote



<MR> wrote in message news:rc0fo29igfb822hs8daarostu35fq09t5r (AT) 4ax (DOT) com...

Quote:
I am wondering, if it is possible to pass an anonymous ary to a function?

Not that I know of, no.


Gambit
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Tue Dec 19, 2006 11:14 pm    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote



<MR> writes:

Quote:
I am wondering, if it is possible to pass an anonymous ary to a
function?

Having a function declaration

void fct(int[]);

the following forks fine:

int a[]={1,2,3};
fct(a);

Can I pass {1,2,3} somehow without temporarily storing it inside a
var?

No. Can you give an example that shows why this would be useful?


Quote:
~~~~~~~~~~

As a following step I would like to pass the number of the
array-indices

void fct(int ary[], size_t ary_size);

The working non-anonymouse-var-version would be

#define TUPEL_ARY_SIZE(ary) ary, sizeof(ary)/sizeof(*ary)

int a[]={1,2,3};
fct(TUPEL_ARY_SIZE(a));

Have you looked at std::vector and/or Boost.Array
(cf. http://www.boost.org/)?


Quote:
It's just a tryout: if the result looks too ugly, I would use the
working version above...

I would advise against it.

Consider

void g(int ints[], std::size_t nrOfInts)
{
}

void f(int ints[])
{
g(TUPEL_ARY_SIZE(ints)); // passes wrong value for nrOfInts
}

int main()
{
int ints[] = {1,2,3};
f(ints);
}


If anything, I nowadays prefer to use something like

#include <cstddef>

template <typename T, std::size_t N>
char (&array_sizer(T const (&)[N]))[N];

void g(int ints[], std::size_t nrOfInts)
{
}

void f(int ints[])
{
g(ints, sizeof array_sizer(ints)); // compiler error; ints is not an array
}

int main()
{
int ints[] = {1,2,3};
f(ints);
g(ints, sizeof array_sizer(ints)); // ok
}


array_sizer is a function taking as argument a reference to an array
of N Ts; it's return type is a reference to an array of N chars;
sizeof such a char array is N, and since sizeof is evaluated at
compile time, array_sizer isn't called and needn't be defined.
Back to top
Alex Bakaev [TeamB]
Guest





PostPosted: Wed Dec 20, 2006 1:06 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote

Thomas Maeder [TeamB] wrote:

Quote:
template <typename T, std::size_t N
char (&array_sizer(T const (&)[N]))[N];

Both bcc and comeau complain that this function doesn't return a value.
VC++ doesn't. I don't know what gcc says.

..a

p.s. I've used this for quite some time and find it very useful
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Wed Dec 20, 2006 1:40 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote

"Alex Bakaev [TeamB]" <zxtt (AT) att (DOT) net> writes:

Quote:
Thomas Maeder [TeamB] wrote:

template <typename T, std::size_t N
char (&array_sizer(T const (&)[N]))[N];

Both bcc and comeau complain that this function doesn't return a
value. VC++ doesn't. I don't know what gcc says.

.a

Strange, Comeau compiles it cleanly for me. What version are you
using? Is it possible that the warning comes from the underlying C
compiler?

--
Chris (TeamB);
Back to top
Duane Hebert
Guest





PostPosted: Wed Dec 20, 2006 2:05 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote

"Alex Bakaev [TeamB]" <zxtt (AT) att (DOT) net> wrote in message
news:458837ea (AT) newsgroups (DOT) borland.com...
Quote:
Thomas Maeder [TeamB] wrote:

template <typename T, std::size_t N
char (&array_sizer(T const (&)[N]))[N];

Both bcc and comeau complain that this function doesn't return a value.
VC++ doesn't. I don't know what gcc says.

Comeau online test compiles it cleanly for me after adding the
include:

#include <cstddef>
template <typename T, std::size_t N>
char (&array_sizer(T const (&)[N]))[N];

int main() {}

Your Comeau C/C++ test results are as follows:

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:4Cool for ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing. All rights reserved.
MODE:strict errors C++

In strict mode, with -tused, Compile succeeded (but remember, the Comeau
online compiler does not link).
Back to top
Alex Bakaev [TeamB]
Guest





PostPosted: Wed Dec 20, 2006 7:01 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote

Duane Hebert wrote:
Quote:

Comeau online test compiles it cleanly for me after adding the


Here it is:
"ComeauTest.c", line 3: warning: missing return statement at end of non-void
function "char_array_size(const T (&)[N]) [with T=T, N=N]"
char const (& char_array_size( T const (&)[N] ) ) [N] { }


code:

template <typename T, const size_t N>
char const (& char_array_size( T const (&)[N] ) ) [N] { }


I see the difference. In my case it's actual function with the body.

Case solved :)

..a
Back to top
Duane Hebert
Guest





PostPosted: Wed Dec 20, 2006 7:18 am    Post subject: Re: Is it possible to pass an anonymous ary to a function? Reply with quote

"Alex Bakaev [TeamB]" <zxtt (AT) att (DOT) net> wrote in message
news:45888b2d$1 (AT) newsgroups (DOT) borland.com...
Quote:
Duane Hebert wrote:

Comeau online test compiles it cleanly for me after adding the


Here it is:
"ComeauTest.c", line 3: warning: missing return statement at end of
non-void
function "char_array_size(const T (&)[N]) [with T=T, N=N]"
char const (& char_array_size( T const (&)[N] ) ) [N] { }


code:

template <typename T, const size_t N
char const (& char_array_size( T const (&)[N] ) ) [N] { }


I see the difference. In my case it's actual function with the body.

Case solved Smile

Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.