Click here to Skip to main content
15,892,480 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWhen to use Dialog for app and when to use normal Window ? Pin
Defenestration6-Mar-08 13:46
Defenestration6-Mar-08 13:46 
AnswerRe: When to use Dialog for app and when to use normal Window ? Pin
Mark Salsbery6-Mar-08 14:56
Mark Salsbery6-Mar-08 14:56 
AnswerRe: When to use Dialog for app and when to use normal Window ? Pin
JudyL_MD7-Mar-08 1:54
JudyL_MD7-Mar-08 1:54 
QuestionHow does RtlpNumberOf work? Pin
Glenn Sandoval6-Mar-08 12:29
Glenn Sandoval6-Mar-08 12:29 
GeneralRe: How does RtlpNumberOf work? Pin
Randor 6-Mar-08 12:48
professional Randor 6-Mar-08 12:48 
GeneralRe: How does RtlpNumberOf work? Pin
Mark Salsbery6-Mar-08 12:52
Mark Salsbery6-Mar-08 12:52 
GeneralRe: How does RtlpNumberOf work? Pin
Mark Salsbery6-Mar-08 13:14
Mark Salsbery6-Mar-08 13:14 
AnswerRe: How does RtlpNumberOf work? [modified] Pin
Rajkumar R7-Mar-08 3:24
Rajkumar R7-Mar-08 3:24 
Glenn Sandoval wrote:
Can anyone explain this in terms mere mortals can understand?

i think the following is too descriptive for mere mortals.

Glenn Sandoval wrote:
I found this magical bit of code that will give the number of elements in an array without knowing it's type.

I can see three magics. 1 ) one taking advantage of sizeof(char) as 1, 2)taking advantage of const parameter in template declaration to deduce the size of array.
3) sizeof() operator only bothers type, it won't execute function

Glenn Sandoval wrote:

template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];

#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))


1) use of sizeof(char)
(*RtlpNumberOf(A)) effectively has the return type as the charater array type with the equal number of elements that of the Array passed, since sizeof (char) is 1, the sizeof (character array) will be the number of elements.

ie; if you pass <array of anytype>[128], then return type of RtlpNumberOf is char(*)[128], so sizeof(* char(*)[128]) = sizeof(char[128]) = 128 * sizeof(char) = 128

char (*function())[10]; function returning pointer to character array(as function cannot return array so pointer to array)
sizeof(*function()) = sizeof(*char(*)[10]) = sizeof(char[10]) = 10 * sizeof(char) = 10;
// note sizeof operator uses dereferenced return type. (*function())

2) use of const parameter in template
template <size_t N>
void func(char (&arr)[N]); // function taking reference to array.
here the constant parameter N is deduced to number of element of array.
so char cArray[128]; func(cArray) will deduce N as 128;


putting it togetter,
template <typename T, size_t N>
char (*func(T (& )[N]))[N] 
func has return type char (*)[N] which sizeof() operator considers.


3) use of sizeof() operator won't execute function.
So definition of function is not needed. at the point of template instantiation, N is deduced to the number of element of array, function is not executed, returns the number of elements.

advantage

advantage of this over sizeof(array)/sizeof(array[0]) is,
1) unnamed struct cannot be used.
2) pointer which may not be an array cannot be used.

modified on Friday, March 7, 2008 10:11 AM

GeneralRe: How does RtlpNumberOf work? Pin
Glenn Sandoval7-Mar-08 4:59
Glenn Sandoval7-Mar-08 4:59 
GeneralCustom action DLL for Windows Installer Pin
act_x6-Mar-08 11:15
act_x6-Mar-08 11:15 
GeneralCrypto++ & FileSink newbie question Pin
soul7106-Mar-08 11:14
soul7106-Mar-08 11:14 
GeneralActiveX question Pin
AbbyIndian6-Mar-08 7:15
AbbyIndian6-Mar-08 7:15 
GeneralRe: ActiveX question Pin
Ernest Laurentin6-Mar-08 7:18
Ernest Laurentin6-Mar-08 7:18 
QuestionHow to place an ActiveX control on a dialog box of a DLL Pin
Deelip6-Mar-08 6:32
Deelip6-Mar-08 6:32 
AnswerRe: How to place an ActiveX control on a dialog box of a DLL Pin
Ernest Laurentin6-Mar-08 7:16
Ernest Laurentin6-Mar-08 7:16 
GeneralRe: How to place an ActiveX control on a dialog box of a DLL Pin
Deelip6-Mar-08 18:19
Deelip6-Mar-08 18:19 
GeneralDLL persistent storage "new" operator Pin
ForNow6-Mar-08 6:23
ForNow6-Mar-08 6:23 
QuestionRe: DLL persistent storage "new" operator Pin
CPallini6-Mar-08 21:32
mveCPallini6-Mar-08 21:32 
GeneralRe: DLL persistent storage "new" operator Pin
ForNow6-Mar-08 23:03
ForNow6-Mar-08 23:03 
GeneralRe: DLL persistent storage "new" operator Pin
CPallini6-Mar-08 23:11
mveCPallini6-Mar-08 23:11 
GeneralRe: DLL persistent storage "new" operator Pin
ForNow7-Mar-08 6:22
ForNow7-Mar-08 6:22 
GeneralObjects outside/free of the form Pin
codeinelogic6-Mar-08 5:43
codeinelogic6-Mar-08 5:43 
GeneralRe: Objects outside/free of the form Pin
led mike6-Mar-08 5:52
led mike6-Mar-08 5:52 
GeneralRe: Objects outside/free of the form Pin
CPallini6-Mar-08 5:52
mveCPallini6-Mar-08 5:52 
QuestionHow do I prevent multiple instances of my program Pin
cgb1436-Mar-08 5:28
cgb1436-Mar-08 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.