473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading sizeof operator

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
9 15285
On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal <fa*******@gmai l.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
Faisal <fa*******@gmai l.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
On Jun 18, 8:58 pm, "Alf P. Steinbach" <al...@start.no wrote:
* Lilith:
On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal
<faisal...@gmai l.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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
In article <4d08b6b2-d195-471b-be09-df95071f7299
@k37g2000hsf.go oglegroups.com> , ja*********@gma il.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
On Jun 18, 12:30*pm, Faisal <faisal...@gmai l.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
On 18 Jun., 18:30, Faisal <faisal...@gmai l.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
On 19 Jun, 23:48, Vyacheslav Kononenko
<vyacheslav.kon one...@gmail.co mwrote:
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<typena me T>
struct MySizeOf
{
enum { value = sizeof(T) };
};

struct SomeType
{};

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

DP
Jun 27 '08 #8
On Jun 20, 2:24*am, Triple-DES <DenPlettf...@g mail.comwrote:
On 19 Jun, 23:48, Vyacheslav Kononenko

<vyacheslav.kon one...@gmail.co mwrote:
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<typena me T>
struct MySizeOf
{
* enum { value = sizeof(T) };

};

struct SomeType
{};

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

};
Good point. Thanks.
Jun 27 '08 #9
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::size of() member function? Yet it may confuse
other programmers it would just provide what you want.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2596
by: David.H | last post by:
Good evening everyone, Im using this code to get an idea on overloading the + operator: class Graph { public: Graph(void); Graph(int valX, int valY); Graph operator+(const Graph&); int getx(void) { return x; }
36
2279
by: JS | last post by:
I read in K&R page 204 that sizeof use on a char returns 1. But when I write the following I get 4! int main(void){ printf("%d\n",sizeof('g')); } Was it not supposed to return 1?
5
2238
by: Aloo | last post by:
dear fellas, this is anupam aka aloo and i have small problem the problem ststes :-- >> IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data without using 'sizeof' operator. >> One soluton is that if the pointer to the data is given then even if we don't know the type we cn just increment the pointer and see by how much it is incremented as the c compiler itself increments it according to its size.
12
36649
by: Christopher Pragash | last post by:
Hello All, Is there an equivalent of SizeOf operator in VB.NET? What I'm trying to achieve is to determine at runtime what the size of hashtable is in terms of the memory it occupies. Thanks in advance, chris
12
1886
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
12
2421
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
4
17180
by: vijay | last post by:
hi, if you see assembly then sizeof operator has sizeof(type) or sizeof(variable) at compile time. How does C compiler gets value at compiler time.? How can we implement sizeof operator? the implementation as macro is as below #define sizeof_op1(val) (&val +1 ) - &val // for variable ex
6
2641
by: Daniel Rudy | last post by:
Hello Everyone, I've ran into a little snag with the sizeof operator. Can you get the size of a member inside a struct using sizeof? I looked through the FAQ and I couldn't find the answer to this one. Below is the code fragment. Granted it's pretty platform specific. typedef struct hwata_info_tag
2
2780
by: Sarath | last post by:
Hello All Is it possible to overload typeid operator? In my understanding it's not possible Could you please provide more information on same? Regards, Sarath
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.