An overloaded operator& needs the address of its argument

Discussion in 'C++' started by Angel Tsankov, May 9, 2007.

  1. How can an overloaded operator& take the address of its argument:

    template<typename T>
    Smth operator &(T& SomeObject)
    {
    // The address of SomeObject is needed here
    }
     
    Angel Tsankov, May 9, 2007
    #1
    1. Advertising

  2. "Angel Tsankov" <-sofia.bg> wrote in message
    news:f1sd1q$6jg$...
    > How can an overloaded operator& take the address of its argument:
    >
    > template<typename T>
    > Smth operator &(T& SomeObject)
    > {
    > // The address of SomeObject is needed here
    > }
    >


    It's argument is always 'this', as you can't define the unary & as a
    non-member. And since 'this' is a pointer, you already have it's address :)

    - Sylvester Hesp
     
    Sylvester Hesp, May 9, 2007
    #2
    1. Advertising

  3. --
    Angel Tsankov
    -sofia.bg
    "Sylvester Hesp" <> wrote in message
    news:4641bcac$0$336$4all.nl...
    > "Angel Tsankov" <-sofia.bg> wrote in message
    > news:f1sd1q$6jg$...
    >> How can an overloaded operator& take the address of its
    >> argument:
    >>
    >> template<typename T>
    >> Smth operator &(T& SomeObject)
    >> {
    >> // The address of SomeObject is needed here
    >> }
    >>

    >
    > It's argument is always 'this', as you can't define the unary &
    > as a non-member. And since 'this' is a pointer, you already
    > have it's address :)


    Does the standard say that unary address-of operator must be a
    member? If so, where?
     
    Angel Tsankov, May 9, 2007
    #3
  4. "Angel Tsankov" <-sofia.bg> wrote in message
    news:f1siu4$ppu$...
    >
    >
    > --
    > Angel Tsankov
    > -sofia.bg
    > "Sylvester Hesp" <> wrote in message
    > news:4641bcac$0$336$4all.nl...
    >> "Angel Tsankov" <-sofia.bg> wrote in message
    >> news:f1sd1q$6jg$...
    >>> How can an overloaded operator& take the address of its argument:
    >>>
    >>> template<typename T>
    >>> Smth operator &(T& SomeObject)
    >>> {
    >>> // The address of SomeObject is needed here
    >>> }
    >>>

    >>
    >> It's argument is always 'this', as you can't define the unary & as a
    >> non-member. And since 'this' is a pointer, you already have it's address
    >> :)

    >
    > Does the standard say that unary address-of operator must be a member? If
    > so, where?


    You're absolutely right, I was mistaken.
    You could take the address by using a reinterpret_cast to a primitive type
    on which the unary & does what you want. boost::addressof does it like that:

    template<class T> T* addressof(T& t)
    {
    return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
    volatile char&>(t)));
    }

    - Sylvester Hesp
     
    Sylvester Hesp, May 9, 2007
    #4
  5. Angel Tsankov

    Jim Langston Guest

    "Sylvester Hesp" <> wrote in message
    news:4641d13c$0$322$4all.nl...
    >
    > "Angel Tsankov" <-sofia.bg> wrote in message
    > news:f1siu4$ppu$...
    >>
    >>
    >> --
    >> Angel Tsankov
    >> -sofia.bg
    >> "Sylvester Hesp" <> wrote in message
    >> news:4641bcac$0$336$4all.nl...
    >>> "Angel Tsankov" <-sofia.bg> wrote in message
    >>> news:f1sd1q$6jg$...
    >>>> How can an overloaded operator& take the address of its argument:
    >>>>
    >>>> template<typename T>
    >>>> Smth operator &(T& SomeObject)
    >>>> {
    >>>> // The address of SomeObject is needed here
    >>>> }
    >>>>
    >>>
    >>> It's argument is always 'this', as you can't define the unary & as a
    >>> non-member. And since 'this' is a pointer, you already have it's address
    >>> :)

    >>
    >> Does the standard say that unary address-of operator must be a member? If
    >> so, where?

    >
    > You're absolutely right, I was mistaken.
    > You could take the address by using a reinterpret_cast to a primitive type
    > on which the unary & does what you want. boost::addressof does it like
    > that:
    >
    > template<class T> T* addressof(T& t)
    > {
    > return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
    > volatile char&>(t)));
    > }


    I must be missing something. Why wouldn't

    template<class T> T* addressof(T& t)
    {
    return &*t;
    }

    work?
     
    Jim Langston, May 10, 2007
    #5
  6. Angel Tsankov

    Greg Herlihy Guest

    On May 9, 10:02 pm, "Jim Langston" <> wrote:
    > "Sylvester Hesp" <> wrote in message
    >
    > news:4641d13c$0$322$4all.nl...
    >
    >
    >
    >
    >
    > > "Angel Tsankov" <-sofia.bg> wrote in message
    > >news:f1siu4$ppu$...

    >
    > >> --
    > >> Angel Tsankov
    > >> -sofia.bg
    > >> "Sylvester Hesp" <> wrote in message
    > >>news:4641bcac$0$336$4all.nl...
    > >>> "Angel Tsankov" <-sofia.bg> wrote in message
    > >>>news:f1sd1q$6jg$...
    > >>>> How can an overloaded operator& take the address of its argument:

    ....
    > >> Does the standard say that unary address-of operator must be a member? If
    > >> so, where?

    >
    > > You're absolutely right, I was mistaken.
    > > You could take the address by using a reinterpret_cast to a primitive type
    > > on which the unary & does what you want. boost::addressof does it like
    > > that:

    >
    > > template<class T> T* addressof(T& t)
    > > {
    > > return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
    > > volatile char&>(t)));
    > > }

    >
    > I must be missing something. Why wouldn't
    >
    > template<class T> T* addressof(T& t)
    > {
    > return &*t;
    > }


    Try it out, say, with an int:

    template <int> int * addressof(int& t)
    {
    return &*t; // Error: invalid type argument of 'unary *'
    }

    Greg
     
    Greg Herlihy, May 10, 2007
    #6
  7. On 10 Maj, 07:02, "Jim Langston" <> wrote:
    > "Sylvester Hesp" <> wrote in message
    >
    > news:4641d13c$0$322$4all.nl...
    >
    >
    >
    >
    >
    > > "Angel Tsankov" <-sofia.bg> wrote in message
    > >news:f1siu4$ppu$...

    >
    > >> --
    > >> Angel Tsankov
    > >> -sofia.bg
    > >> "Sylvester Hesp" <> wrote in message
    > >>news:4641bcac$0$336$4all.nl...
    > >>> "Angel Tsankov" <-sofia.bg> wrote in message
    > >>>news:f1sd1q$6jg$...
    > >>>> How can an overloaded operator& take the address of its argument:

    >
    > >>>> template<typename T>
    > >>>> Smth operator &(T& SomeObject)
    > >>>> {
    > >>>> // The address of SomeObject is needed here
    > >>>> }

    >
    > >>> It's argument is always 'this', as you can't define the unary & as a
    > >>> non-member. And since 'this' is a pointer, you already have it's address
    > >>> :)

    >
    > >> Does the standard say that unary address-of operator must be a member? If
    > >> so, where?

    >
    > > You're absolutely right, I was mistaken.
    > > You could take the address by using a reinterpret_cast to a primitive type
    > > on which the unary & does what you want. boost::addressof does it like
    > > that:

    >
    > > template<class T> T* addressof(T& t)
    > > {
    > > return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
    > > volatile char&>(t)));
    > > }

    >
    > I must be missing something. Why wouldn't
    >
    > template<class T> T* addressof(T& t)
    > {
    > return &*t;
    > }
    >
    > work?


    First you dereference t (which means that T must either be a pointer
    of implement operator *) and then you take the address of what was
    returned. So if T was a normal pointer then you would return a copy of
    t right?

    However since the return-type is T* this does not compile for normal
    pointers, nor for builtin functions. The only thing I can see this
    working for is something like this:

    struct Foo {
    Foo& operator*() {return *this;}
    };

    I think you must have forgotten something in your previous post.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 10, 2007
    #7
    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. glen stark
    Replies:
    3
    Views:
    535
    glen stark
    Sep 30, 2003
  2. , India
    Replies:
    8
    Views:
    445
    James Kanze
    Jul 19, 2010
  3. thunk
    Replies:
    1
    Views:
    318
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    491
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    629
    thunk
    Apr 3, 2010
Loading...

Share This Page