By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

An overloaded operator& needs the address of its argument

P: n/a
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
}

May 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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
May 9 '07 #2

P: n/a


--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>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?

May 9 '07 #3

P: n/a

"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
>"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>>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 TT* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpre t_cast<const
volatile char&>(t)));
}

- Sylvester Hesp
May 9 '07 #4

P: n/a
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
>
"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>>

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
>>"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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 TT* 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 TT* addressof(T& t)
{
return &*t;
}

work?
May 10 '07 #5

P: n/a
On May 9, 10:02 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message

news:46*********************@news.xs4all.nl...


"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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 TT* 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 TT* addressof(T& t)
{
return &*t;
}
Try it out, say, with an int:

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

Greg

May 10 '07 #6

P: n/a
On 10 Maj, 07:02, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message

news:46*********************@news.xs4all.nl...


"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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 TT* 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 TT* 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

May 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.