C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

lengthof(array) for VS6

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Teh (tî'pô)
Guest





PostPosted: Wed Apr 21, 2004 7:34 pm    Post subject: lengthof(array) for VS6 Reply with quote



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





PostPosted: Thu Apr 22, 2004 3:07 pm    Post subject: Re: lengthof(array) for VS6 Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.