sizeof operator
-
Dear All, The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type. Can any body suggest how to make our own function that operates like sizeof and gives the amount of storage in bytes. Regards, Rohit
-
Dear All, The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type. Can any body suggest how to make our own function that operates like sizeof and gives the amount of storage in bytes. Regards, Rohit
Untested, but here's an idea:
template<typename T> size_t Sizeof()
{
T* p1 = (T*) 100, *p2 = p1+1;return (BYTE*) p2 - (BYTE*) p1;
}size_t n = Sizeof<int>();
--Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Laugh it up, fuzzball.
-
Untested, but here's an idea:
template<typename T> size_t Sizeof()
{
T* p1 = (T*) 100, *p2 = p1+1;return (BYTE*) p2 - (BYTE*) p1;
}size_t n = Sizeof<int>();
--Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Laugh it up, fuzzball.
What's wrong with:
template <typename T> size_t Sizeof() {
return sizeof T;
}For completeness I'd add
template <typename T> struct size_of : public std::generator<size_t> {
size_t operator()() {
return sizeof T;
}
};and/or
template <typename T> struct size_of : public std::unary_function<size_t, T> {
size_t operator()(const T& v) {
return sizeof v;
}
};-- Booohoo!
-
What's wrong with:
template <typename T> size_t Sizeof() {
return sizeof T;
}For completeness I'd add
template <typename T> struct size_of : public std::generator<size_t> {
size_t operator()() {
return sizeof T;
}
};and/or
template <typename T> struct size_of : public std::unary_function<size_t, T> {
size_t operator()(const T& v) {
return sizeof v;
}
};-- Booohoo!
I thought the original question was how to make your own
sizeof
operator. If you usesizeof
in yourSizeof()
it doesn't count ;P --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- You cannot truly appreciate Dilbert unless you've read it in the original Klingon.