Overloading sizeof operator

Discussion in 'C++' started by Faisal, Jun 18, 2008.

  1. Faisal

    Faisal Guest

    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.
     
    Faisal, Jun 18, 2008
    #1
    1. Advertising

  2. Faisal

    James Kanze Guest

    On Jun 18, 8:58 pm, "Alf P. Steinbach" <> wrote:
    > * Lilith:


    > > On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal
    > > <> wrote:


    > >> 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:
    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
     
    James Kanze, Jun 18, 2008
    #2
    1. Advertising

  3. Faisal

    Jerry Coffin Guest

    In article <4d08b6b2-d195-471b-be09-df95071f7299
    @k37g2000hsf.googlegroups.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.
     
    Jerry Coffin, Jun 19, 2008
    #3
  4. Faisal

    Faisal Guest

    On Jun 18, 11:58 pm, "Alf P. Steinbach" <> wrote:
    > * Lilith:
    >
    > > On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal <>
    > > 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?

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

    > 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.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Faisal, Jun 19, 2008
    #4
  5. On Jun 18, 12:30 pm, Faisal <> 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.


    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;
    }
    -------------------------------------------------
     
    Vyacheslav Kononenko, Jun 19, 2008
    #5
  6. Faisal

    Guest

    On 18 Jun., 18:30, Faisal <> wrote:
    > 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 19, 2008
    #6
  7. Faisal

    Triple-DES Guest

    On 19 Jun, 23:48, Vyacheslav Kononenko
    <> wrote:
    > 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
     
    Triple-DES, Jun 20, 2008
    #7
  8. On Jun 20, 2:24 am, Triple-DES <> wrote:
    > On 19 Jun, 23:48, Vyacheslav Kononenko
    >
    >
    >
    > <> wrote:
    > > 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.
     
    Vyacheslav Kononenko, Jun 23, 2008
    #8
  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::sizeof() member function? Yet it may confuse
    other programmers it would just provide what you want.
     
    Laurent D.A.M. MENTEN, Jun 23, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,417
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    662
    CBFalconer
    Apr 10, 2004
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,632
    Keith Thompson
    Feb 14, 2005
  4. Vinu
    Replies:
    13
    Views:
    1,502
    Lawrence Kirby
    May 12, 2005
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    714
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page