Board index » cppbuilder » Re: Getting the size of an array?

Re: Getting the size of an array?


2005-07-20 09:58:33 PM
cppbuilder25
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
"Hendrik Schober" < XXXX@XXXXX.COM >writes:

>Ed Mulroy < XXXX@XXXXX.COM >wrote:
>>Chris correctly told what was the missing word in my sentence. He also
>>showed code exactly demonstrating the two situations about which I was
>>concerned. Chris is getting scary - he's beginning to read my mind!! <g>
>
>Hey, maybe Chris actually is your wife
>in disguise? :^>

I can assure you that that conjecture is incorrect!
That's exactly what I expected you to answer! <g>
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 
 

Re:Re: Getting the size of an array?

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
[...]
But it's still a useful technique, though in every case where it
works, sizeof can be used too. What's nicer about the template than
using sizeof(ary)/sizeof(elt) is that in contexts where it's not an
array, it won't compile, but the sizeof technique will compile and
silently give the wrong value.
In fact, we use this in our code:
template< typename T, std::size_t sz>
inline T* end(T (&array)[sz])
{
return array + sz;
}
Having an array, this allows to nicely interface
with the STL:
int ai[10] = ...;
int* pi = ai;
std::sort( ai, end(ai) );
std::sort( pi, end(pi) ); // compile error
This I consider a lot easier to read than
std::sort( ai, ai + sizeof(ai)/sizeof(ai[0]) );
(Actually, just for even easier reading, I even
have a fake 'begin()'
template< typename T, std::size_t sz>
inline T* begin(T (&array)[sz])
{
return array;
}
so that I can write
std::sort( begin(ai), end(ai) );
which everyone understands immediately. <g>)
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:Re: Getting the size of an array?

"Hendrik Schober" < XXXX@XXXXX.COM >writes:
Quote
In fact, we use this in our code:


template< typename T, std::size_t sz>
inline T* end(T (&array)[sz])
{
return array + sz;
}

Having an array, this allows to nicely interface
with the STL:

int ai[10] = ...;
int* pi = ai;
std::sort( ai, end(ai) );
std::sort( pi, end(pi) ); // compile error
...
Hey, I like that!
--
Chris (TeamB);
 

{smallsort}

Re:Re: Getting the size of an array?

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
[...]
Hey, I like that!
Of course, this wasn't my idea, but Dietmar Kuehl's:
groups.google.com/groups
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:Re: Getting the size of an array?

Hendrik Schober wrote:
Quote
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:

>[...]
>But it's still a useful technique, though in every case where it
>works, sizeof can be used too. What's nicer about the template than
>using sizeof(ary)/sizeof(elt) is that in contexts where it's not an
>array, it won't compile, but the sizeof technique will compile and
>silently give the wrong value.


In fact, we use this in our code:


template< typename T, std::size_t sz>
inline T* end(T (&array)[sz])
{
return array + sz;
}
Another useful one is the compile-time array size:
template <typename T, std::size_t sz>
char (&array_length_impl(T (&array)[sz]))[sz]; // no definition needed
#define ARRAY_LENGTH(ar) (sizeof array_sizeof_impl(ar))
or something like that (hopefully my declaration is ok). Then you can do:
int array[10];
double parallelarray[ARRAY_LENGTH(array)];
That's probably also Dietmar Kuehl's idea - I don't remember for sure
though.
Tom
 

Re:Re: Getting the size of an array?

Tom Widmer < XXXX@XXXXX.COM >writes:
Quote
template <typename T, std::size_t sz>
char (&array_length_impl(T (&array)[sz]))[sz]; // no definition needed

#define ARRAY_LENGTH(ar) (sizeof array_sizeof_impl(ar))

or something like that (hopefully my declaration is ok). Then you can do:
That's (to me) a new twist on the old metaprogramming truck used to
see if a condition is true or not.
Quote
That's probably also Dietmar Kuehl's idea - I don't remember for sure
though.
It's a clever trick, regardless. :) Still, I think I'd rather use the
class approach simply because it avoids the problems a macro can
introduce, even if it requires typing a few more characters to extract
the value.
--
Chris (TeamB);
 

Re:Re: Getting the size of an array?

"Hendrik Schober" < XXXX@XXXXX.COM >wrote in message news:42de58ea$ XXXX@XXXXX.COM ...
Quote
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
>"Hendrik Schober" < XXXX@XXXXX.COM >writes:
>
>>Ed Mulroy < XXXX@XXXXX.COM >wrote:
>>>Chris correctly told what was the missing word in my sentence. He also
>>>showed code exactly demonstrating the two situations about which I was
>>>concerned. Chris is getting scary - he's beginning to read my mind!! <g>
>>
>>Hey, maybe Chris actually is your wife
>>in disguise? :^>
>
>I can assure you that that conjecture is incorrect!


That's exactly what I expected you to answer! <g>
Sure. But the way those guys argue sometime, one can
understand the confusion <g>
 

Re:Re: Getting the size of an array?

Tom Widmer < XXXX@XXXXX.COM >wrote:
Quote
[...]
Another useful one is the compile-time array size:

template <typename T, std::size_t sz>
char (&array_length_impl(T (&array)[sz]))[sz]; // no definition needed

#define ARRAY_LENGTH(ar) (sizeof array_sizeof_impl(ar))

or something like that (hopefully my declaration is ok). [...]
<brr>
I had to look at this for five minutes just to
understand what it's doing and why it's doing it
this way. Why not using 'size_of<arr>::result'?
At least that's sticking to TMP convention and
more widely understood. Am I missing something?
Quote
Tom
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:Re: Getting the size of an array?

Hendrik Schober wrote:
Quote
Tom Widmer < XXXX@XXXXX.COM >wrote:

>[...]
>Another useful one is the compile-time array size:
>
>template <typename T, std::size_t sz>
>char (&array_length_impl(T (&array)[sz]))[sz]; // no definition needed
>
>#define ARRAY_LENGTH(ar) (sizeof array_sizeof_impl(ar))
That should have been
#define ARRAY_LENGTH(ar) (sizeof array_length_impl(ar))
of course.
Quote
>
>or something like that (hopefully my declaration is ok). [...]


<brr>
I had to look at this for five minutes just to
understand what it's doing and why it's doing it
this way. Why not using 'size_of<arr>::result'?
arr has to be a type for that, whereas the macro accepts an object,
unless I'm mis-guessing what the definition of size_of looks like?
Tom
 

Re:Re: Getting the size of an array?

Tom Widmer < XXXX@XXXXX.COM >wrote:
Quote
[...]
>>template <typename T, std::size_t sz>
>>char (&array_length_impl(T (&array)[sz]))[sz]; // no definition needed
[...]
#define ARRAY_LENGTH(ar) (sizeof array_length_impl(ar))

[...]

><brr>
>I had to look at this for five minutes just to
>understand what it's doing and why it's doing it
>this way. Why not using 'size_of<arr>::result'?

arr has to be a type for that, whereas the macro accepts an object,
unless I'm mis-guessing what the definition of size_of looks like?
Dooh! You're certainly right. We have
template< typename T, std::size_t sz>
inline std::size_t size(T (&array)[sz])
{
return sz;
}
but this doesn't yield a compile-time constant.
Makes me appreciate your solution. Although I
still don't really understand why it works.
Quote
Tom
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:Re: Getting the size of an array?

"Hendrik Schober" < XXXX@XXXXX.COM >writes:
Quote
but this doesn't yield a compile-time constant.
Makes me appreciate your solution. Although I
still don't really understand why it works.
The function is declared to return an array of chars, and the sizeof
the array is deteremined by the size of the template argument. The
weird part about it is that the array dimensions are to the "right" of
the function arguments, while the array type is to the left. Since
sizeof a char is always 1, the size of the whole array is the correct
value.
But that's consistent with normal placement of array dimension
syntax. It's just really hard to read.
--
Chris (TeamB);
 

Re:Re: Getting the size of an array?

"Duane Hebert" < XXXX@XXXXX.COM >writes:
Quote
>>I can assure you that that conjecture is incorrect!
>
>
>That's exactly what I expected you to answer! <g>

Sure. But the way those guys argue sometime, one can
understand the confusion <g>
We're not a married couple. (Sometimes we just emulate one.)
--
Chris (TeamB);
 

Re:Re: Getting the size of an array?

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
We're not a married couple. (Sometimes we just emulate one.)
:-)
 

Re:Re: Getting the size of an array?

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
[...]
The function is declared to return an array of chars, and the sizeof
the array is deteremined by the size of the template argument. The
weird part about it is that the array dimensions are to the "right" of
the function arguments, while the array type is to the left. [...]
That's the declaration for a function returning
an array??? No way I would have found that out...
Thanks Chris.
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:Re: Getting the size of an array?

"Hendrik Schober" < XXXX@XXXXX.COM >writes:
Quote
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
>[...]
>The function is declared to return an array of chars, and the sizeof
>the array is deteremined by the size of the template argument. The
>weird part about it is that the array dimensions are to the "right" of
>the function arguments, while the array type is to the left. [...]


That's the declaration for a function returning
an array??? No way I would have found that out...

Thanks Chris.
Sorry, I made a slight, yet important, mistake here.
You're not allowed to declare a function that returns an array, but
you are allowed to return a REFERENCE to an array. That's what this
function was doing. For example:
char foo()[10]; // Error, function can't return array
but
char (&foo())[10]; // Ok, returns a reference to an array
The part that is really hard to grok is that the & is for the return
value and not that we're declaring a reference to a function.
--
Chris (TeamB);