Overloading sizeof operator

F

Faisal

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.
 
J

James Kanze

* Lilith:
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.
 
J

Jerry Coffin

[ ... ]
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...
 
F

Faisal

* Lilith:



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.

Hi Alf,
Could you please elaborate this little more?
I couldn't get the actual problem.
 
V

Vyacheslav Kononenko

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;
}
-------------------------------------------------
 
T

thomas.mertes

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.
 
T

Triple-DES

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
 
V

Vyacheslav Kononenko

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.
 
L

Laurent D.A.M. MENTEN

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,176
Latest member
Jerilyn201
Top