 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Teh (tî'pô) Guest
|
Posted: Wed Apr 21, 2004 7:34 pm Post subject: lengthof(array) for VS6 |
|
|
While searching this group I came across this implementation for
lengthof(array) for VS6.
template<typename T>
void lengthof(T* const, T* const*);
template<typename T>
int lengthof(const void*, T);
#define lengthof(a)
(sizeof(lengthof(a, &(a))) ? sizeof(a) / sizeof((a)[0]) : 0)
I don't get it. Could someone please spell out why the void function
is a better match for pointers and the int one for arrays?
Please use monosyllabic words as this construct has made me feel
particularly stupid...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen Ludin Guest
|
Posted: Thu Apr 22, 2004 3:07 pm Post subject: Re: lengthof(array) for VS6 |
|
|
Teh (tî'pô) wrote:
| Quote: | While searching this group I came across this implementation for
lengthof(array) for VS6.
template
void lengthof(T* const, T* const*);
template
int lengthof(const void*, T);
#define lengthof(a)
(sizeof(lengthof(a, &(a))) ? sizeof(a) / sizeof((a)[0]) : 0)
I don't get it. Could someone please spell out why the void function
is a better match for pointers and the int one for arrays?
|
The compiler error for gcc 3.3 gives a hint at it:
length.cpp:22: error: ISO C++ says that `void lengthof(T*, T* const*)
[with T = int]' and `int lengthof(const void*, T) [with T = int**]'
are ambiguous even though the worst conversion for the former is
better than the worst conversion for the latter
The 'trick' is the array can only resolve to one template function and
the pointer version however can resolve to either. So this works not
because the 'void function is a better match for pointers', but
rather because they are both (roughly) equally valid for pointers,
hence the compiler error.
There is no ambiguity for the array version since &T[n] cannot be
passed into a T *const * parameter.
-stephen
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|