473,325 Members | 2,771 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,325 software developers and data experts.

Array size Template and references to array

Hi all,
I was just browsing the archives when I saw one fo the messages posted
recently about array size template et al.

All along, I have been using the usual template to get the size of the
array.
template<typename T, std::size_t N>
std::size_t size_of(T(&)[N])
{
return N;
}

But as one person pointed out, the following definition fails with the
above template, which is what I have always noticed when I use similar
definitions.
int x[5];
int y[size_of(x)]; //fails here

I always went back to the simple way in these definition
int y[sizeof(x)/sizeof(*x)]; //the simple way.

The person who pointed out the error, also had the template that
works.I would like to use this template all the time, but I am not
quite sure, what it is exactly doing. It is trying to get a reference
to an array and then get the size of that array(which itself is a
reference to a char)..Not quite sure. Can somebody explain ?

template <typename T, unsigned N>
char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ];

#define Nelem( A ) (sizeof( NelemArrayFunc( A ) ))

Thanks.

Aug 3 '05 #1
5 6426
I also didn't know that it was possible to do like this.
int x[5];
int y[size_of(x)]; //fails here
for me it works with g++, but doesn't with ms vc71;
.....
basicly
template <typename T, unsigned N>
char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ];

means declaration of a function that takes a reference to array of N
T's (T[N]) and returns a reference to array of N chars. (references are
used to preseve type info)

and then macro
#define Nelem( A ) (sizeof( NelemArrayFunc( A ) ))
uses sizeof NelemArrayFunc( A ); where NelemArrayFunc is only valid for
arrays. When this function used as an argument to sizeof the only
information used is the function's return type, that is for T[N] it
returns char[N], and sizeof(char[N]) is N

NelemArrayFunc could be written also like this:
template <typename T, unsigned N>
char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ]{
char x[N];
return x;
}
Between, anyone has a good link for info on declaration of functions
(as for such unusual declaration) and also declaration of pointers to
such functions. For example, how would I need to declare pointer to
NelemArrayFunc???

thanks

Aug 3 '05 #2
I think the problem is that when you pass a raw array T arr[N] to a
function as an argument the array "degrades" to a pointer T* arr. Therefore
part of the type information (N) is lost. This, however, is not the case
with type passed to class templates. And therefore with some odd looking
code we can implement size_of this way:

template <typename T, int N>
class size_calc;

template <typename T, int N>
class size_calc<T[N]>
{
public:
enum {size = N};
};

template <typename T>
int size_of(const T& t)
{
return size_calc<T>::size;
}

Notice the declaration of size_of hides the fact that t is an array from the
compiler, and hence prevents the type degrading.
Aug 4 '05 #3
My previous post is completely stupid. Forget it.

Here is what I should have posted:

template <typename T, int N>
int size_of (const T (&arr) [N])
{
return N;
}
Aug 4 '05 #4
__PPS__ wrote:
....


Between, anyone has a good link for info on declaration of functions
(as for such unusual declaration) and also declaration of pointers to
such functions. For example, how would I need to declare pointer to
NelemArrayFunc???


template <typename T, unsigned N>
char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ];

char ( & (* XNelemArrayFunc)( int ( & )[ 3 ] ) )[ 3 ];

XNelemArrayFunc = & NelemArrayFunc<int, 3>;
Aug 6 '05 #5
benben wrote:
I think the problem is that when you pass a raw array T arr[N] to a
function as an argument the array "degrades" to a pointer T* arr. Therefore
part of the type information (N) is lost. This, however, is not the case
with type passed to class templates. And therefore with some odd looking
code we can implement size_of this way:

template <typename T, int N>
class size_calc;

template <typename T, int N>
class size_calc<T[N]>
{
public:
enum {size = N};
};

template <typename T>
int size_of(const T& t)
{
return size_calc<T>::size;
}

Notice the declaration of size_of hides the fact that t is an array from the
compiler, and hence prevents the type degrading.


return is a run-time expression. sizeof is a compile-time expression so
it can be used wherever a constant is needed.

Aug 6 '05 #6

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

Similar topics

13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
12
by: howa | last post by:
// example #include <iostream> using namespace std; size_t bsearch(int *a) { cout<<a<<endl; cout<<sizeof(a) / sizeof(a)<<endl;
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
9
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
3
by: mani | last post by:
int (*nVar)..This is the variable i used in a function.. i tried ...nothing worked.. anyone please tell me how to return it....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.