Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > An overloaded operator& needs the address of its argument

Reply
Thread Tools

An overloaded operator& needs the address of its argument

 
 
Angel Tsankov
Guest
Posts: n/a
 
      05-09-2007
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
}

 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      05-09-2007
"Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
news:f1sd1q$6jg$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Angel Tsankov
Guest
Posts: n/a
 
      05-09-2007


--
Angel Tsankov
http://www.velocityreviews.com/forums/(E-Mail Removed)-sofia.bg
"Sylvester Hesp" <(E-Mail Removed)> wrote in message
news:4641bcac$0$336$(E-Mail Removed)4all.nl...
> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> news:f1sd1q$6jg$(E-Mail Removed)...
>> 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?

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      05-09-2007

"Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
news:f1siu4$ppu$(E-Mail Removed)...
>
>
> --
> Angel Tsankov
> (E-Mail Removed)-sofia.bg
> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
> news:4641bcac$0$336$(E-Mail Removed)4all.nl...
>> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
>> news:f1sd1q$6jg$(E-Mail Removed)...
>>> 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&>(reinterpre t_cast<const
volatile char&>(t)));
}

- Sylvester Hesp


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      05-10-2007
"Sylvester Hesp" <(E-Mail Removed)> wrote in message
news:4641d13c$0$322$(E-Mail Removed)4all.nl...
>
> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> news:f1siu4$ppu$(E-Mail Removed)...
>>
>>
>> --
>> Angel Tsankov
>> (E-Mail Removed)-sofia.bg
>> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
>> news:4641bcac$0$336$(E-Mail Removed)4all.nl...
>>> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
>>> news:f1sd1q$6jg$(E-Mail Removed)...
>>>> 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&>(reinterpre t_cast<const
> volatile char&>(t)));
> }


I must be missing something. Why wouldn't

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

work?


 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      05-10-2007
On May 9, 10:02 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
>
> news:4641d13c$0$322$(E-Mail Removed)4all.nl...
>
>
>
>
>
> > "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> >news:f1siu4$ppu$(E-Mail Removed)...

>
> >> --
> >> Angel Tsankov
> >> (E-Mail Removed)-sofia.bg
> >> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
> >>news:4641bcac$0$336$(E-Mail Removed)4all.nl...
> >>> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> >>>news:f1sd1q$6jg$(E-Mail Removed)...
> >>>> 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&>(reinterpre t_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

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-10-2007
On 10 Maj, 07:02, "Jim Langston" <(E-Mail Removed)> wrote:
> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
>
> news:4641d13c$0$322$(E-Mail Removed)4all.nl...
>
>
>
>
>
> > "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> >news:f1siu4$ppu$(E-Mail Removed)...

>
> >> --
> >> Angel Tsankov
> >> (E-Mail Removed)-sofia.bg
> >> "Sylvester Hesp" <(E-Mail Removed)> wrote in message
> >>news:4641bcac$0$336$(E-Mail Removed)4all.nl...
> >>> "Angel Tsankov" <(E-Mail Removed)-sofia.bg> wrote in message
> >>>news:f1sd1q$6jg$(E-Mail Removed)...
> >>>> 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&>(reinterpre t_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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deducing the argument type of overloaded function call operator subramanian100in@yahoo.com, India C++ 8 07-19-2010 05:24 PM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Passing a function pointer as an argument to an overloaded >> operator. glen stark C++ 3 09-30-2003 08:25 AM



Advertisments