I know C++ and know the function sizeof itself but I need to write my own sizeof function so please explain how it works exactly? What does it do with the parameter?
I know C++ and know the function sizeof itself but I need to write
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You haven’t provided any meaningful details about what it is you want to do, so it is hard to figure out what you need.
You can “wrap”
sizeofby you own template function likeand then use it as
From time to time one can come across a request to implement sizeof-like functionality without using
sizeof. Requests like that make no practical sense whatsoever, yet sometimes are used as homework assignments. One can probably do it as followswhich would require a default-constructible
T. A less restricting but formally illegal solution (a hack) would be something likeThe above implementations implement type-based
sizeof.An attempt to emulate the functionality of value-based
sizeofmight look as followsbut this will not be even remotely equivalent to the built-in
sizeof, at least because the built-insizeofdoes not evaluate its argument.Finally, neither of these implementations will produce integral constant expressions (ICE), as the built-in
sizeofdoes. Producing an ICE that way is impossible to achieve in the current version of the language.In any case this all, of course, is totally devoid of any practical value. Just use
sizeofwhen you want to know the size.