By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,193 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,193 IT Pros & Developers. It's quick & easy.

Overloading sizeof operator

P: n/a
Hi,

Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the type userdefined types.

For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.

Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal <fa*******@gmail.com>
wrote:
>Hi,

Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the type userdefined types.

For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.
Is it because it's more like a compile time operator than a runtime?

--
Lilith
Jun 27 '08 #2

P: n/a
Faisal <fa*******@gmail.comkirjutas:
Hi,

Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the type userdefined types.

For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.
sizeof() operator essentially gives the step in bytes between the adjacent
objects if they are placed in an array. Dynamic data does not fit into this
scheme.

hth
Paavo
Jun 27 '08 #3

P: n/a
On Jun 18, 8:58 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Lilith:
On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal
<faisal...@gmail.comwrote:
Why C++ doesn't allow overloading of size of operator. I
think it would be much handy to check the type userdefined
types.
For eg. In my project, I've some structures which contains
dynamic data( pointers). So if i have some way to overload
the sizeof operator I can calculate the exact size and
return.
Is it because it's more like a compile time operator than a
runtime?
No, it's because some operations need to be basic and not have
their meaning changed by programmers, so that it's possible to
always get a handle on things.
You mean like the address of operator:-).

Seriously, I'm not too sure that you're really saying anything
different that what Faisal said. One of the essential
characteristics of sizeof is that the results can be used in an
integral constant expression. The fact that VLA's would break
this property was one of the arguments (not the only one)
against adopting them in C++. And maintaining this
characteristic would be very difficult if overloading it were
allowed.
Without some guaranteed not redefinable basic mechanisms we'd
be lost. The OP can just provide his own size() operation or
whatever, no need to redefine the built-in basic one, which
should not have its meaning redefined ever.
I totally agree. But I think the same argumentation would apply
to the address of operator. The two most fundamental
characteristic of an object (in the sense of the word used in
the standard) are its address and its size.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

P: n/a
In article <4d08b6b2-d195-471b-be09-df95071f7299
@k37g2000hsf.googlegroups.com>, ja*********@gmail.com says...

[ ... ]
Seriously, I'm not too sure that you're really saying anything
different that what Faisal said. One of the essential
characteristics of sizeof is that the results can be used in an
integral constant expression. The fact that VLA's would break
this property was one of the arguments (not the only one)
against adopting them in C++. And maintaining this
characteristic would be very difficult if overloading it were
allowed.
Though it could (perhaps) be retained if you required the overloaded
sizeof to be/return a constexpr...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5

P: n/a
On Jun 18, 12:30*pm, Faisal <faisal...@gmail.comwrote:
Hi,

Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the *type userdefined types.

For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.
I wonder how you would imagine that (let's say this syntax works):
struct MyStruct {
size_t sizeof() const { return mydatasize; }
private:
size_t mydatasize;
};

std::cout << sizeof( MyStruct ); // how would it work here ???

If you always want to call it for an object not for type you do not
need to screw up standard operator sizeof:

------------------------------------------
#include <iostream>

template< class T >
size_t mySizeof( const T & ) { return sizeof( T ); }

class Foo {
public:
virtual size_t fancySizeof() const;
};

template<>
size_t mySizeof( const Foo &f ) { return f.fancySizeof(); }

int main()
{
Foo foo;
int i;

std::cout << mySizeof( i ) << mySizeof( foo ) << std::endl;

return 0;
}
-------------------------------------------------
Jun 27 '08 #6

P: n/a
On 18 Jun., 18:30, Faisal <faisal...@gmail.comwrote:
Hi,

Why C++ doesn't allow overloading of size of operator.
What about

#define sizeof mySizeof

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
Jun 27 '08 #7

P: n/a
On 19 Jun, 23:48, Vyacheslav Kononenko
<vyacheslav.konone...@gmail.comwrote:
If you always want to call it for an object not for type you do not
need to screw up standard operator sizeof:

------------------------------------------
#include <iostream>

template< class T >
size_t mySizeof( const T & ) { return sizeof( T ); }

class Foo {
* *public:
* * * virtual size_t fancySizeof() const;

};

template<>
size_t mySizeof( const Foo &f ) { return f.fancySizeof(); }

int main()
{
* *Foo foo;
* *int i;

* *std::cout << mySizeof( i ) << mySizeof( foo ) << std::endl;

* *return 0;}

-------------------------------------------------
This wouldn't work if you want to use your custom sizeof in a constant
expression. If that's the case, you could do something like:

template<typename T>
struct MySizeOf
{
enum { value = sizeof(T) };
};

struct SomeType
{};

template<>
struct MySizeOf<SomeType>
{
enum{ value = 4 };
};

DP
Jun 27 '08 #8

P: n/a
On Jun 20, 2:24*am, Triple-DES <DenPlettf...@gmail.comwrote:
On 19 Jun, 23:48, Vyacheslav Kononenko

<vyacheslav.konone...@gmail.comwrote:
If you always want to call it for an object not for type you do not
need to screw up standard operator sizeof:
------------------------------------------
#include <iostream>
template< class T >
size_t mySizeof( const T & ) { return sizeof( T ); }
class Foo {
* *public:
* * * virtual size_t fancySizeof() const;
};
template<>
size_t mySizeof( const Foo &f ) { return f.fancySizeof(); }
int main()
{
* *Foo foo;
* *int i;
* *std::cout << mySizeof( i ) << mySizeof( foo ) << std::endl;
* *return 0;}
-------------------------------------------------

This wouldn't work if you want to use your custom sizeof in a constant
expression. If that's the case, you could do something like:

template<typename T>
struct MySizeOf
{
* enum { value = sizeof(T) };

};

struct SomeType
{};

template<>
struct MySizeOf<SomeType>
{
* enum{ value = 4 };

};
Good point. Thanks.
Jun 27 '08 #9

P: n/a
Faisal a écrit :
Hi,

Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the type userdefined types.

For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.
And what about a someClass::sizeof() member function? Yet it may confuse
other programmers it would just provide what you want.
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.